POJ 1019 Number Sequence
2015-07-21 12:14
471 查看
描述
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.
样例输入
2
8
3
样例输出
2
2
题目跟之前的不大一样 之前是1-9的重复 这个是1-N的来
数学问题 转载的。。方法很简单 动笔写写就容易明白
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.
样例输入
2
8
3
样例输出
2
2
题目跟之前的不大一样 之前是1-9的重复 这个是1-N的来
数学问题 转载的。。方法很简单 动笔写写就容易明白
#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,就得到2 int main() { int t,n; scanf("%d",&t); Set(); while(t--) { scanf("%d",&n); printf("%d\n",Compute(n)); } return 0; }
相关文章推荐
- UITableView以及cell属性
- guestmount
- CodeForces 484B Maximum Value
- IOS学习笔记UIButton
- guestfish 修改 image file
- 设置UIView几个角为圆角
- YUI Compressor使用配置方法 JS/CSS压缩工具
- iOS7中计算UILabel中字符串的高度
- iOS-Core-Animation-Advanced-Techniques(六)
- iOS-Core-Animation-Advanced-Techniques(四)
- iOS-Core-Animation-Advanced-Techniques(一)
- 【xib】加载的一些问题,以UICollectionView 为例
- ContentResolver query 参数详解
- 【EasyUI】——EasyUI的布局
- iOS 中如何除去UIAlertView 规避delegate对象销毁后接收消息的crash
- @RequestParam @RequestBody @PathVariable 等参数绑定注解详解
- Android UI 之一步步教你自定义控件
- AFJSONRequestSerializer
- ORA-02287: sequence number not allowed here解决
- ios NSURLSession completeHandler默认调用quque