1019. Number Sequence
来源:互联网 发布:java流程管理系统 编辑:程序博客网 时间:2024/06/15 21:38
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
代码
/*通过打表可以发现,一共可以分为31268个数据串。最长的一串有145236位*/#include <iostream>#include <math.h>#include <string.h>using namespace std;int len1[31270] = { 0 }; unsigned int len2[31270] = { 0 }; void group(){ int i = 0; len1[1] = 1; len2[1] = 1; for (i = 2;i < 31270; i++) { len1[i] = len1[i - 1] + (int)log10(i*1.0) + 1; len2[i] = len2[i - 1] + len1[i]; }}int main(){ int t = 0; int j = 1,k = 1; int MAX[145240] = { 0 }; group(); for (j = 1; j < 31270; j++) { char str[10]; int a = 0; int temp; temp = j; while (temp) { str[a++] = temp % 10 + '0'; temp /= 10; } //确定最长的一串 while (a--) { MAX[k++] = str[a] - '0'; } } cin >> t; while (t--) { int num = 0, i = 0; cin >> i; for (j = 0; j < 31270; j++) { if (len2[j] >= i) break; } num = MAX[i - len2[j - 1]]; cout << num << endl; } return 0;}
- 1019. Number Sequence
- POJ 1019.Number Sequence
- Number Sequence
- Number Sequence
- Number Sequence
- Number Sequence
- Number Sequence
- Number Sequence
- Number Sequence
- Number Sequence
- Number Sequence
- Number Sequence
- Number Sequence
- Number Sequence
- Number Sequence
- Number Sequence
- Number Sequence
- Number Sequence
- 是时候思考一下到底要不要干程序员了
- nohup和&后台运行,进程查看及终止
- table 添加表行的底部边框、定制表格边框
- HDU6047 Maximum Sequence(思路)
- Mac平台重新设置MySQL的root密码
- 1019. Number Sequence
- 语义分析相关应用的实作要点
- 欢迎使用CSDN-markdown编辑器
- Spring引入Hibernate
- sessionStorage
- linux下面利用shell在指定的行添加内容
- 多位数字用/和%取得每位数的方法
- “已超过传入消息(65536)的最大消息大小配额。若要增加配额,请使用相应绑定元素上的 MaxReceivedMessageSize 属性
- mybatis配置文件中${}和#{}