Any positive integer can be written as the sum of several consecutive integers. For example,
15 = 1 + . . . + 5 = 4 + . . . + 6 = 7 + . . . + 8 = 15 + . . . + 15
Given a positive integer n, what are the consecutive positive integers with sum being n? If there are multiple solutions, which one consists of more numbers?
Input
Input consists of multiple problem instances. Each instance consists of a single positive integer n, where n ≤ 10^9. The input data is terminated by a line containing ‘-1’. There will be at most 1000 test cases.
Output
For each input integer n, print out the desired solution with the format:
N = A + … + B
in a single line. (Read sample output for a clearer representation of the exact formatting.)
Sample Input
8
15
35
-1
Sample Output
8 = 8 + … + 8
15 = 1 + … + 5
35 = 2 + … + 8
问题链接:UVA11254 Consecutive Integers
问题简述:(略)
问题分析:
数学题,暴力枚举一下就好了,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)
AC的C++语言程序如下:
/* UVA11254 Consecutive Integers */
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
while(~scanf("%d", &n) && n !=-1) {
int start = 1;
int end = sqrt(2.0 * n);
for(int i = 2; i <=end; i++) {
if(2 * n % i == 0 && (2 * n / i + i) % 2 == 1) {
if(2 * n / i < i + 1)
break;
start = i;
}
}
int a = (2 * n / start - start + 1) / 2;
int b = (2 * n / start + start - 1) / 2;
printf("%d = %d + ... + %d\n", n, a, b);
}
return 0;
}