九度 和为S的连续正数序列

题目1354:和为S的连续正数序列

时间限制:2 秒

内存限制:32 兆

特殊判题:

提交:2008

解决:622

题目描述:

小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100。但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数)。没多久,他就得到另一组连续正数和为100的序列:18,19,20,21,22。现在把问题交给你,你能不能也很快的找出所有和为S的连续正数序列? Good Luck!

输入:

输入有多组数据。

每组数据仅包括1个整数S(S<=1,000,000)。如果S为负数时,则结束输入。

输出:

对应每组数据,若不存在和为S的连续正数序列,则输出“Pity!”;否则,按照开始数字从小到大的顺序,输出所有和为S的连续正数序列。每组数据末尾以“#”号结束。

样例输入:
4
5
100
-1
样例输出:
Pity!
#
2 3
#
9 10 11 12 13 14 15 16
18 19 20 21 22
#


题解:(m+n)(n-m+1)/2=N;令m+n=i;然后找n-m+1

代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 #define mem(x,y) memset(x,y,sizeof(x))
 7 using namespace std;
 8 const int INF=0x3f3f3f3f;
 9 typedef long long LL;
10 int X,Y;
11 int main(){
12     LL N,m,n;
13     while(scanf("%lld",&N),N>=0){
14         int flot=0;
15          for(int i=sqrt(2*N);i>=2;i--){
16              if((2*N)%i==0){
17                  n=(-1+i+2*N/i)/2;
18                  m=2*N/i-n;
19              //    printf("%d %d %d
",i,m,n);
20                  if((n+m)*(n-m+1)!=2*N)continue;
21                  flot=1;
22                  for(int i=m;i<=n;i++){
23                      if(i-m)printf(" ");
24                      printf("%d",i);
25                  }
26                  puts("");
27              }
28          }
29          if(!flot)puts("Pity!
#");
30          else puts("#");
31     }
32     return 0;
33 }

 第二种解法,很好:

代码:、

 1 /*#include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 #define mem(x,y) memset(x,y,sizeof(x))
 7 using namespace std;
 8 const int INF=0x3f3f3f3f;
 9 typedef long long LL;
10 int X,Y;
11 int main(){
12     LL N,m,n;
13     while(scanf("%lld",&N),N>=0){
14         int flot=0;
15          for(int i=sqrt(2*N);i>=2;i--){
16              if((2*N)%i==0){
17                  n=(-1+i+2*N/i)/2;
18                  m=2*N/i-n;
19              //    printf("%d %d %d
",i,m,n);
20                  if((n+m)*(n-m+1)!=2*N)continue;
21                  flot=1;
22                  for(int i=m;i<=n;i++){
23                      if(i-m)printf(" ");
24                      printf("%d",i);
25                  }
26                  puts("");
27              }
28          }
29          if(!flot)puts("Pity!
#");
30          else puts("#");
31     }
32     return 0;
33 }*/
34 #include<iostream>
35 #include<cstdio>
36 #include<cstring>
37 #include<cmath>
38 #include<algorithm>
39 #define mem(x,y) memset(x,y,sizeof(x))
40 using namespace std;
41 const int INF=0x3f3f3f3f;
42 typedef long long LL;
43 int main(){
44     int T;
45     int N;
46     while(scanf("%d",&N),N>=0){
47         int l=1,r=2;
48         int sum=3;
49         int flot=0;
50         while(l<r){
51             if(sum==N){
52                 flot=1;
53                 for(int i=l;i<=r;i++){
54                     if(i-l)printf(" ");
55                     printf("%d",i);
56                 }puts("");
57                 r++;
58                 sum+=r;
59             }
60             if(sum>N){
61                 sum-=l;
62                 l++;
63             }
64             else{
65                 r++;
66                 sum+=r;
67             }
68         }
69         if(!flot)puts("Pity!
#");
70         else puts("#");
71     }
72     return 0;
73 }
原文地址:https://www.cnblogs.com/handsomecui/p/4947557.html