6 1 2 3 4 5 10
1 1 2 1 2 4解题思路:先找出第n个数在Si,这通过公式i(i+1)/2=n解得i=(2n+0.25)0.5-0.5,此处需将i向上取整而不是向下取整。则第n个数是在Si 中的第n-Si-1个数 ,再mod 9 即可得结果对于S8以上的数据直接通过查表得结果源代码:#include <stdio.h> #include <math.h> #include <stdlib.h> int main() { int k; double i; unsigned long j,n; int s[]={0,1,1,2,1,2,3,1,2,3,4,1,2,3,4,5,1,2,3,4,5,6,1,2,3,4,5,6,7, 1,2,3,4,5,6,7,8}; scanf("%d",&k); while(k--) { scanf("%ld",&n); if(n<=36) printf("%d\n",s[n]); else { i=ceil(sqrt(2*n*1.0+0.25)-0.5); j=(unsigned long)i-1; n=n-j*(j+1)/2; // printf("i=%lf\n j=%ld\n n1=%ld\n n2=%ld\n",i,j,j*(j+1)/2,n); if(n%9) printf("%d\n",n%9); else printf("9\n"); } } system("pause"); return 0; }
原文地址:http://blog.csdn.net/zchlww/article/details/42706851