POJ 1019 Number Sequence
来源:互联网 发布:单片机控制继电器 编辑:程序博客网 时间:2024/06/05 01:41
描述
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
输入
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)
输出
There should be one output line per test case containing the digit located in the position i.
样例输入
8
3
样例输出
2
#include<iostream>#include<cstdio>#include<cmath>using namespace std;const int maxn=31269;unsigned a[maxn];unsigned s[maxn];void Set(){ a[1]=1,s[1]=1; for(int i=2;i<maxn;i++){ a[i]=a[i-1]+(int)log10((double)i)+1; s[i]=s[i-1]+a[i];//表示第i组数字列的长度 } }int Compute(int n){ int i=1; while(s[i]<n) i++; int pos=n-s[i-1];//答案在s[i-1]里的第pos个 int len=0; for(i=1;len<pos;i++) len+=(int)log10((double)i)+1;//找到对应len的位置 return (i-1)/(int)pow((double)10,len-pos)%10;//此时的i-1便是具体的值 答案在i里 去除尾巴}//取出1234的2,那么多余的位数有2位:34。那么用1234 / 10^2,得到12,再对12取模10,就得到2int main(){ int t,n; scanf("%d",&t); Set(); while(t--){ scanf("%d",&n); printf("%d\n",Compute(n)); } return 0;}
- POJ 1019 Number Sequence
- POJ 1019 Number Sequence
- poj 1019 Number Sequence
- poj 1019 Number Sequence
- POJ 1019 Number Sequence
- poj 1019 Number Sequence
- POJ-1019 Number Sequence
- POJ 1019 Number Sequence
- poj 1019 Number Sequence
- poj 1019 Number Sequence
- POJ 1019 Number Sequence
- poj 1019 Number Sequence
- POJ 1019 Number Sequence
- POJ 1019 Number Sequence
- poj 1019 Number Sequence
- POJ 1019 Number Sequence
- poj 1019 Number Sequence
- POJ 1019 Number Sequence
- 欢迎使用CSDN-markdown编辑器
- Address already in use:JVM_Bind
- 二叉树创建、删除、(递归/非递归)先序(中序/后序)遍历
- 利用VBScript在隐藏窗口中运行应用程序
- C中的位运算
- POJ 1019 Number Sequence
- linux下把文件夹压缩成tar.gz的命令
- Java-LinkedList
- 对高效管理时间的个人观点分享
- Android--小米奇葩bug
- 帧动画关键帧处加入触发函数
- OpenMesh学习记录 1
- EventBus篇一:ThreadMode
- FLEX通过ANE调用安卓 ExtensionContext.createExtensionContext 返回null