Codevs 1312 连续自然数和

1312 连续自然数和

题目描述 Description

对于一个自然数M,求出所有的连续的自然数段,使得这些连续自然数段的全部数字和为M.
eg:1998+1999+2000+2001+2002=10000,所以从1998到2002的一个自然数段为M=10000的一个解。 

输入描述 Input Description

一个数M

输出描述 Output Description

每行两个数,为连续自然数段的一头一尾,所有输出行的第一个数按照升序排列

样例输入 Sample Input

10000

样例输出 Sample Output

18 142

297 328

388 412

1998 2002

/*枚举起点判断行不行,好像还算快的*/
#include<iostream>
#include<cstdio>
using namespace std;
int n;
int main(){
    int s,t;
    scanf("%d",&n);
    for(int i=1;i<n;i++){
        s=n;t=i;
        while(s>0)s-=t,t++;
        if(s==0)printf("%d %d
",i,t-1);
    }
}
原文地址:https://www.cnblogs.com/thmyl/p/7077270.html