http://acm.hdu.edu.cn/showproblem.php?pid=1597

数学问题,找规律,

令n=x*(x+1)/2;配方移项化简的x=sqrt((double) 2*n+0.25)-0.5
 求出前x项的和与n比较判断。
 注意n的范围用__int64

 

1597
1 #include<stdio.h>
2 #include<string.h>
3 #include<stdlib.h>
4 #include<math.h>
5  #define max(x,y) (x)>(y)?x:y
6  #define min(x,y) (x)<(y)?x:y
7 #define N 10001
8 int main()
9 {
10 int K;
11 __int64 n,x,y,z;
12 scanf("%d",&K);
13 while(K--)
14 {
15 scanf("%I64d",&n);
16 x=(__int64)(sqrt((double) 2*n+0.25)-0.5);
17 if(n==x*(x+1)/2)x--;
18 y=x*(x+1)/2;
19 z=(n-y)%9;
20 if((n-y)%9==0)z=9;
21 printf("%I64d\n",z);
22 }
23 return 0;
24 }

 

posted on 2010-10-09 23:06  role  阅读(410)  评论(0编辑  收藏  举报