Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
靠,这么简单的题目也会有问题!不要用模拟法。还是数学方法好。//模拟法,SAPLE 中的10000和1000不对,为什么? #include <stdio.h> #include<stdlib.h> int main() { int j=1,index; int a[100001]; //a=(int *)malloc(101*sizeof(int)); for(index=1;index<100001;index+=(j-1)) { int J; for(J=0;J<j;J++) a[index+J]=j; j++; } for(index=2;index<10001;index++) a[index]+=a[index-1]; while(1) { scanf("%d",&index); if(!index) break; printf("%d\n",a[index]); } return 0; } //数学方法,AC了 #include <stdio.h> int fun(int x) { int n; for(n=1;;n++) if(2*x<=(n*(n+1))) { int r; n--; r=n*(n+1)*(2*n+1)/6+(x-n*(n+1)/2)*(n+1); return r; } } int main() { int x,res; while(1) { scanf("%d",&x); if(!x) break; res=fun(x); printf("%d %d\n",x,res); } } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator