首页 > 其他 > 详细

庞果网之高斯公式

时间:2014-06-09 22:05:11      阅读:364      评论:0      收藏:0      [点我收藏+]

【题目】

题目详情

高斯在上小学时发明了等差数列求和公式:1+2+..+100=5050。如今问题在于给你一个正整数n,问你他能够表示为多少种连续正整数之和?(自身也算)。

输入格式:

多组数据,每组数据一行,一个正整数n。 0<n<2000000000

输出格式:

每组数据一行,包括一个正整数,表示结果。


答题说明

输入例子

5

120

输出例子:

2

4

解释:

5=2+3=5

120=1+2+...+15=22+23+24+25+26=39+40+41=120


【分析】

详细详见:点击打开链接

【代码】

/*********************************
*   日期:2014-04-26
*   作者:SJF0115
*   题目: 高斯公式
*   来源:http://hero.csdn.net/Question/Details?ID=537&ExamID=532
*   结果:AC
*   来源:庞果网
*   总结:
**********************************/
#include <iostream>
#include <stdio.h>
using namespace std;

int main(){
    int n,i;
    while(scanf("%d",&n) != EOF){
        int small = 1;
        int big = 2;
        int mid = (1 + n) / 2;
        int count = 0;
        int cur = small + big;
        while(small < mid){
            //cur = n
            if(cur == n){
                count ++;
            }
            //cur > n
            while(cur > n && small < mid){
                cur -= small;
                small ++;
                if(cur == n){
                    count++;
                }
            }//while
            //cur < n
            big ++;
            cur += big;
        }//while
        printf("%d\n",count+1);
    }
    return 0;
}


庞果网之高斯公式,布布扣,bubuko.com

庞果网之高斯公式

原文:http://www.cnblogs.com/hrhguanli/p/3776231.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!