1 #include <iostream> 2 using namespace std; 3 4 long long int pow(long long int base, long long int exp) 5 { 6 long long int result = 1; 7 for(long long int i = 0; i < exp; i++) { 8 result *= base; 9 } 10 return result; 11 } 12 13 int find(long long pos) 14 { 15 long long sum = 0, last_sum = 0; 16 int k = 1; 17 int result = 0; 18 19 while(true) { 20 last_sum = sum; 21 sum += 9 * pow(10, k-1) * k; 22 if(sum > pos) { 23 long long diff = pos - last_sum; 24 long long rank = diff / k; 25 long long mod = diff % k; 26 if(k == 1) result = diff + 1; 27 else result = ((pow(10, k-1) + rank)/pow(10, k-1-mod)) % 10; 28 break; 29 } 30 k++; 31 } 32 33 return result; 34 } 35 36 int main() 37 { 38 int t; 39 long long i, group_num, member_size, sum, last_sum, last_group_sum, group_sum, pos, res; 40 cin >> t; 41 for(int j = 0; j < t; j++) { 42 group_num = 1; 43 last_group_sum = 0; 44 member_size = 1; 45 sum = 0; 46 cin >> i; 47 i = i - 1; 48 while(true){ 49 last_sum = sum; 50 group_sum = last_group_sum + member_size; 51 sum += group_sum; 52 if(sum > i) { 53 res = find(i - last_sum); 54 cout<<res<<endl; 55 break; 56 } 57 last_group_sum = group_sum; 58 group_num++; 59 if(group_num/pow(10, member_size) - (group_num-1)/pow(10, member_size) == 1) member_size++; 60 } 61 } 62 return 0; 63 }
Number Sequence POJ 1019,布布扣,bubuko.com
原文:http://www.cnblogs.com/li-qiang/p/3754261.html