pku1019

 
Number Sequence
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 16917 Accepted: 4450

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

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

题目大意就是从序列112123412345...中查找特定某一位的数字。

代码很ws...不知道如何减小内存使用。。
#include <iostream> using namespace std; /* #include <fstream> ifstream fin("data.txt"); #define cin fin */ const int MAXNUM = 31269; unsigned int dsumsum[MAXNUM]; //dsumsum[i]表示序列1121231234...i的长度 unsigned int dsum[MAXNUM]; //dsum[i]表示序列1234...i的长度 //获取某个数的位数 int getdigits(int d) { int ans = 0; while (d != 0) { ans++; d = d/10; } return ans; } //给定索引index, 查找在序列1234...n中index对应的数字 int getonedigit(int index) { int i = 1, ans; while (dsum[i] < index) ++i; index = dsum[i] - index; ans = i; while (index != 0) { ans = ans / 10; index--; } return ans % 10; } int main() { int sum=0, i, j, t, n; for (i=1; i<MAXNUM; i++) { sum += getdigits(i); dsum[i] = sum; dsumsum[i] = dsumsum[i-1] + dsum[i]; if (dsumsum[i] > 2147483647) break; } cin >> t; for (i=0; i<t; i++) { cin >> n; for (j=1; j<MAXNUM; j++) //此处可改为二分查找 { if (dsumsum[j] >= n) break; } cout << getonedigit(n - dsumsum[j-1]) << endl; } return 0; }

你可能感兴趣的:(File,Integer,input,each,output)