UVA11254 Consecutive Integers【暴力】

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值