POJ 1019

1 #include <iostream>
2 #include<stdio.h>
3 #include<math.h>
4 using namespace std;
5
6 const int max = 32000;
7 unsigned int a[32000], s[32000];
8
9 void prepare()
10 {
11 int i;
12 a[1] = 1;
13 s[1] = 1;
14 for(i=2; i<32000; i++)
15 {
16 a[i] = a[i-1] + (int)(log10((double)i)) + 1;
17 s[i] = s[i-1] + a[i];
18 }
19 }
20
21 int calc(int number)
22 {
23 int i, location;
24 int length = 0;
25 for(i=1; s[i] < number; i++);
26
27 //number is in the ith subset
28 location = number - s[i-1];
29
30 //number is the ith number in ith subset
31 for(i=1; location>length; ++i)
32 {
33 length += (int)(log10((double)i))+1;
34 }
35
36 //length-location is the redudant numbers for the ith number
37 //for instance: 123, if you want to get 2, length-location would be 1
38 //use 12 mod 10, the result would be 2
39 return (i-1) / (int)(pow((double)10, length-location)) % 10;
40 }
41
42 int main()
43 {
44 prepare();
45 int count = 0;
46 int number = 0;
47 cin >> count;
48 for(int i = 0; i<count; i++)
49 {
50 cin >> number;
51 cout << calc(number) << endl;
52 }
53
54 }
2 #include<stdio.h>
3 #include<math.h>
4 using namespace std;
5
6 const int max = 32000;
7 unsigned int a[32000], s[32000];
8
9 void prepare()
10 {
11 int i;
12 a[1] = 1;
13 s[1] = 1;
14 for(i=2; i<32000; i++)
15 {
16 a[i] = a[i-1] + (int)(log10((double)i)) + 1;
17 s[i] = s[i-1] + a[i];
18 }
19 }
20
21 int calc(int number)
22 {
23 int i, location;
24 int length = 0;
25 for(i=1; s[i] < number; i++);
26
27 //number is in the ith subset
28 location = number - s[i-1];
29
30 //number is the ith number in ith subset
31 for(i=1; location>length; ++i)
32 {
33 length += (int)(log10((double)i))+1;
34 }
35
36 //length-location is the redudant numbers for the ith number
37 //for instance: 123, if you want to get 2, length-location would be 1
38 //use 12 mod 10, the result would be 2
39 return (i-1) / (int)(pow((double)10, length-location)) % 10;
40 }
41
42 int main()
43 {
44 prepare();
45 int count = 0;
46 int number = 0;
47 cin >> count;
48 for(int i = 0; i<count; i++)
49 {
50 cin >> number;
51 cout << calc(number) << endl;
52 }
53
54 }