POJ1019 Number Sequence
Number Sequence
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 36256 | Accepted: 10461 |
Description
A single positive integer i is given. Write a program to find the digit located in the position i in the sequence of number groups S1S2...Sk. Each group Sk consists of a sequence of positive integer numbers ranging from 1 to k, written one after another.
For example, the first 80 digits of the sequence are as follows:
11212312341234512345612345671234567812345678912345678910123456789101112345678910
For example, the first 80 digits of the sequence are as follows:
11212312341234512345612345671234567812345678912345678910123456789101112345678910
Input
The first line of the input file contains a single integer t (1 ≤ t ≤ 10), the number of test cases, followed by one line for each test case. The line for a test case contains the single integer i (1 ≤ i ≤ 2147483647)
Output
There should be one output line per test case containing the digit located in the position i.
Sample Input
2 8 3
Sample Output
2 2
Source
Tehran 2002, First Iran Nationwide Internet Programming Contest
题意就不说了,看下就明白。思路就是打表,然后就是要知道求一个数的位数:(int)log10((double)x)+1
/* ID: LinKArftc PROG: 1019.cpp LANG: C++ */ #include <map> #include <set> #include <cmath> #include <stack> #include <queue> #include <vector> #include <cstdio> #include <string> #include <utility> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define eps 1e-8 #define randin srand((unsigned int)time(NULL)) #define input freopen("input.txt","r",stdin) #define debug(s) cout << "s = " << s << endl; #define outstars cout << "*************" << endl; const double PI = acos(-1.0); const int inf = 0x3f3f3f3f; const int INF = 0x7fffffff; typedef long long ll; const int maxn = 40000; ll sum[maxn], line[maxn];//分别是前i行的位数和,第i行的位数 int getbit(int x) { return (int)log10((double)x) + 1; } void init() { sum[1] = line[1] = 1; for (int i = 2; i <= 35000; i ++) { line[i] = line[i-1] + getbit(i); sum[i] = sum[i-1] + line[i]; } } int getpos(int x, int pos) { int len = getbit(x); for (int i = 1; i <= len - pos; i ++) x /= 10; return x % 10; } int main() { init(); int T; ll n; scanf("%d", &T); while (T --) { scanf("%lld", &n); int cur = 1; while (sum[cur] < n) cur ++; ll pos = n - sum[cur-1]; while (cur >= 1) { if (pos > line[cur-1]) { pos -= line[cur-1]; printf("%d\n", getpos(cur, pos)); break; } else cur --; } } return 0; }
Read The Fucking Source Code ☺!