Problem Description
假设:
S1 = 1
S2 = 12
S3 = 123
S4 = 1234
.........
S9 = 123456789
S10 = 1234567891
S11 = 12345678912
............
S18 = 123456789123456789
..................
现在我们把所有的串连接起来
S = 1121231234.......123456789123456789112345678912.........
那么你能告诉我在S串中的第N个数字是多少吗?
 

Input
输入首先是一个数字K,代表有K次询问。
接下来的K行每行有一个整数N(1 <= N < 2^31)。
 

Output
对于每个N,输出S中第N个对应的数字.
 

Sample Input
6
1
2
3
4
5
10
 

Sample Output
1
1
2
1
2
4
 

Code

The Accepted answer

 1 #include <stdio.h>
 2 void main()
 3 {
 4     int i, c, r, sum, count;
 5     scanf("%d", &count);
 6     while(count --)
 7     {
 8         sum = 0;
 9         scanf("%d", &c);
10         for (i = 0; sum < c; )
11             sum += ++i;
12         sum -= i;
13         r = c - sum;
14         if (r % 9 == 0)
15             printf("9\n");
16         else
17             printf("%d\n", r % 9);
18     }
19 }
20  

The program which I think it's corrct but don't pass the system

 1 #include <stdio.h>
 2 #include <math.h>
 3 void main()
 4 {
 5     int count, n, c, r;
 6     double tempn;
 7     scanf("%d", &count);
 8     while (count --)
 9     {
10         scanf("%d", &c);
11         tempn = sqrt(2 * c + 1.0 / 4) - 1.0 / 2;
12         n = (int)tempn;
13         if (fabs(tempn - n) <= 1e-6)
14             r = n;
15         else 
16             r = c - n * (n + 1) / 2;
17         if (r % 9 == 0)
18             printf("9\n");
19         else
20             printf("%d\n", r % 9);
21     }
22 }

 


 
 

Key Points

Firstly, this is my first time using vi to edit and using gcc to compile. So it's necessary to point out some command.

gcc *.c     then u can get the file named a.out.  which I want to stress is that ur name must end with .c.

./a.out           then will execute the file.

gedit filename   u can look the file with a visualization interface, like txt in windown.

gcc *.c -lm    if ur file contain "#include <math.h>"

Secondly, don't forget "fabs".

 

Recommend
8600
 

posted on 2013-03-13 21:14  MrMission  阅读(351)  评论(0编辑  收藏  举报