Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 13643 | Accepted: 8495 |
本题有一个小小的规律就是1*1+2*2+3*3+.........+(x-1)*(x-1)(x由(x+1)*x/2>n算出)有可能(x+1)*x/2!=n此时令s=(x+1)*x/2;sum=sum+(x-(s-n))*x
代码:
1 #include<stdio.h>
2 int main()
3 {
4 int n,s,i,sum,j;
5 while(scanf("%d",&n)!=EOF)
6 {
7 i=0;s=0;sum=0;
8 if(n==0)
9 break;
10 while(s<n)
11 {
12 i++;
13 s=(i+1)*i/2;
14 }
15 for(j=1;j<i;j++)
16 sum=sum+j*j;
17 sum=sum+(i-s+n)*i;
18 printf("%d %d\n",n,sum);
19 }
20 return 0;
21 }
22