UVa 10427 - Naughty Sleepy Boys

  题目大意:从1开始往后写数字,构成一个如下的字符串 123456789101112... 。求第n位的数字是多少。

  找规律,按数字的位数可以构建一个类似杨辉三角的东西,求出第n位是哪个数的第几位即可。

 1 #include <cstdio>
 2 #include <cmath>
 3 
 4 int main()
 5 {
 6 #ifdef LOCAL
 7     freopen("in", "r", stdin);
 8 #endif
 9     int table[9] = {0};
10     int base = 9;
11     for (int i = 1; i <= 8; i++)
12     {
13         table[i] = i * base;
14         base *= 10;
15     }
16     for (int i = 1; i <= 8; i++)
17         table[i] += table[i-1];
18     int n;
19     while (scanf("%d", &n) != EOF)
20     {
21         int k = 1;
22         while (table[k] < n)   k++;
23         double t1 = 1.0 * (n - table[k-1]) / k;
24         int offset = (int)ceil(t1);  // n is offset-th num of k-th row
25         int t2 = (n - table[k-1] - 1) % k + 1;  // the t2-th digit of the num    
26         int t3 = 0;
27         for (int i  = 1; i <= k-1; i++)
28             t3 = t3*10 + 9;
29         int m = t3 + offset;  // the compete num  
30         for (int i = 0; i < k-t2; i++)   m /= 10;
31         printf("%d\n", m % 10);
32     }
33     return 0;
34 }
View Code

 

posted @ 2013-07-29 01:43  xiaobaibuhei  阅读(336)  评论(0编辑  收藏  举报