UVA 138 Street Numbers
2016-07-16 15:11
246 查看
Description
A computer programmer lives in a street with houses numbered consecutively (from 1) down one side of the street. Every evening she walks her dog by leaving her house and randomly turning left or right and walking to the end of the street and back. One night
she adds up the street numbers of the houses she passes (excluding her own). The next time she walks the other way she repeats this and finds, to her astonishment, that the two sums are the same. Although this is determined in part by her house number and
in part by the number of houses in the street, she nevertheless feels that this is a desirable property for her house to have and decides that all her subsequent houses should exhibit it.
Write a program to find pairs of numbers that satisfy this condition. To start your list the first two pairs are: (house number, last number):
题目的意思是让你找出10组n,k
满足
1+2+...+k-1 == (k+1)+...+n
已知a1,an
用等差数列求和公式Sn=n(a1+an)/2化简比较方便
(k – 1)[1 + (k – 1)] / 2 = (n – k)[(k + 1) + n] / 2,
(2n + 1)2 – 2(2k)2 = 1
k = sqrt(n*(n+1)/2)
n循环+1
判断k不是是整数
是的话就打印出来
输出格式还有要求
宽度为10
#include <cstdio>
#include <cmath>
int main(){
int cnt = 0;
long long n = 6;
double x;
long long y;
while (cnt < 10) {
x = (double) n * (n + 1);
x /= 2;
x = sqrt(x);
y = (int)x;
if (x == y) {
printf("%10lld%10lld\n", y, n);
cnt++;
}
n++;
}
return 0;
}
A computer programmer lives in a street with houses numbered consecutively (from 1) down one side of the street. Every evening she walks her dog by leaving her house and randomly turning left or right and walking to the end of the street and back. One night
she adds up the street numbers of the houses she passes (excluding her own). The next time she walks the other way she repeats this and finds, to her astonishment, that the two sums are the same. Although this is determined in part by her house number and
in part by the number of houses in the street, she nevertheless feels that this is a desirable property for her house to have and decides that all her subsequent houses should exhibit it.
Write a program to find pairs of numbers that satisfy this condition. To start your list the first two pairs are: (house number, last number):
6 8 35 49
Input and Output
There is no input for this program. Output will consist of 10 lines each containing a pair of numbers, each printed right justified in a field of width 10 (as shown above).题目的意思是让你找出10组n,k
满足
1+2+...+k-1 == (k+1)+...+n
已知a1,an
用等差数列求和公式Sn=n(a1+an)/2化简比较方便
(k – 1)[1 + (k – 1)] / 2 = (n – k)[(k + 1) + n] / 2,
(2n + 1)2 – 2(2k)2 = 1
k = sqrt(n*(n+1)/2)
n循环+1
判断k不是是整数
是的话就打印出来
输出格式还有要求
宽度为10
#include <cstdio>
#include <cmath>
int main(){
int cnt = 0;
long long n = 6;
double x;
long long y;
while (cnt < 10) {
x = (double) n * (n + 1);
x /= 2;
x = sqrt(x);
y = (int)x;
if (x == y) {
printf("%10lld%10lld\n", y, n);
cnt++;
}
n++;
}
return 0;
}
相关文章推荐
- 表彰优秀学生(多态)
- Android WebView和JavaScript交互
- Android基础知识---ImageView的状态切换
- 关于css3中@keyframes在火狐浏览器下的兼容问题(我是来提问的)
- 爬虫实战:提取人和老鼠中RNA表达性较好的RNA序列
- N卡双显卡电脑装ubuntu15.04并配置Anaconda+Tensorflow+cuda+cuDNN的深度学习环境
- 杭电ACM 1021
- UIScrollView的子视图的起点不是左上角
- Nginx配置完整例子
- 算法-基于MACD的Adaboost股价涨跌预测模型
- 我是如何在我的unbuntu 虚拟机上安装 配置QT的
- [从头读历史] 第306节 星球战争 BC2799 至 BC2700(公元前28世纪)
- 老王学Linux-云桌面可行性分析
- neural-networks-and-deep-learning mnist_pca.py
- JQuery的ready函数与JS的onload的区别详解
- [JZOJ4638] 第三条跑道(By--A_Crazy_Czy)
- Zxing实现二维码生成,扫描代码段
- hdu 5163 Taking Bus(模拟)
- fsl系统烧写工具MFGTool2工具详解
- 16.7.16 C组总结