POJ1019——Number Sequence(大数处理)
Number Sequence
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
题目大意:
给定一个字符串,构成如下:
1121231234...123456789101112...12345678910111213..N
问字符串的第i位是多少。
解题思路:
将字符串划分成N段。
1 12 123 1234 ... 1234567891011 ... 123456789101112....N
那么对于第K段的长度len[k]=len[k-1]+K这个数字的长度。即len[k]=len[k-1]+log10(k)+1。
再定义sum[k]=sum[k-1]+len[k]。通过比较sum[]与i的大小即可定位到i所在的字段。
假设i在第k个字段(1234567...t....k)中,那么i-sum[k-1]表示的就是i在第k个字段中的位置。
再通过公式log10(j)+1就能判断出i所在的t和在t中的位置pos。
ans=t/pow(10,pos)%10。
Code:
/*************************************************************************
> File Name: poj1019.cpp
> Author: Enumz
> Mail: 369372123@qq.com
> Created Time: 2014年11月08日 星期六 02时33分13秒
************************************************************************/ #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<list>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
#include<bitset>
#include<climits>
#define MAXN 40000
using namespace std;
long long len[MAXN],sum[MAXN];
void init()
{
for (int i=;i<MAXN;i++)
{
len[i]=len[i-]+(int)log10((double)i)+;
sum[i]=sum[i-]+len[i];
}
}
int solve(int N)
{
int i=;
while (sum[i]<N)
i++;
N-=sum[i-];
int len_k=,t;
for(t=;len_k<N;t++)
len_k+=(int)log10((double)t)+;
int pos=len_k-N;
return (t-)/(int)pow((double),pos)%;
}
int main()
{
int T;
cin>>T;
int N;
init();
while (T--)
{
cin>>N;
cout<<solve(N)<<endl;
}
return ;
}
POJ1019——Number Sequence(大数处理)的更多相关文章
- POJ1019 Number Sequence
Number Sequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 36256 Accepted: 10461 ...
- HDU 1005 Number Sequence
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- POJ 1019 Number Sequence
找规律,先找属于第几个循环,再找属于第几个数的第几位...... Number Sequence Time Limit: 1000MS Memory Limit: 10000K Total Submi ...
- HDOJ 1711 Number Sequence
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Number Sequence
Number Sequence A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) ...
- [AX]AX2012 Number sequence framework :(三)再谈Number sequence
AX2012的number sequence framework中引入了两个Scope和segment两个概念,它们的具体作用从下面序列的例子说起. 法国/中国的法律要求财务凭证的Journal nu ...
- KMP - HDU 1711 Number Sequence
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 1005:Number Sequence(水题)
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- Number Sequence 分类: HDU 2015-06-19 20:54 10人阅读 评论(0) 收藏
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
随机推荐
- 结对开发:7-magic用户调研报告
用户调研报告 班级 信1201-2班 学号 20122929 姓名 崔乐乐 项目名称:7-magic 调研时间: 2015/4/14 调研主题:7-magic游戏的用户需求度调研 ...
- MyEclipse 中的各种有的没的快捷方式
快捷键1 (CTRL) Ctrl+1 快速修复Ctrl+D: 删除当前行 Ctrl+Q 定位到最后编辑的地方 Ctrl+L 定位在某行 Ctrl+O 快速显示 OutLine Ctrl ...
- BZOJ3874 codevs3361 宅男计划
AC通道1:http://www.lydsy.com/JudgeOnline/problem.php?id=3874 AC通道2:http://codevs.cn/problem/3361/ [题目分 ...
- 【ContestHunter】【弱省胡策】【Round2】
官方题解:http://wyfcyx.is-programmer.com/posts/95490.html A 目前只会30分的暴力……DP好像很神的样子0.0(听说可以多次随机强行算? //Roun ...
- Leetcode#68 Text Justification
原题地址 没有复杂的算法,纯粹的模拟题 先试探,计算出一行能放几个单词 然后计算出单词之间有几个空格,注意,如果空格总长度无法整除空格数,前面的空格长度通通+1 最后放单词.放空格,组成一行,加入结果 ...
- Leetcode#87 Scramble String
原题地址 两个字符串满足什么条件才称得上是scramble的呢? 如果s1和s2的长度等于1,显然只有s1=s2时才是scramble关系. 如果s1和s2的长度大于1,那么就对s1和s2进行分割,划 ...
- 引擎设计跟踪(九.14.2e) DelayLoaded DLLs (/DELAYLOAD)
关于DLL的delay load: http://msdn.microsoft.com/en-us/library/151kt790.aspx 最近在做GLES的shader compiler, 把现 ...
- boost 相关
编译boost: 1.打开Microsoft Visual Studio 2010 -> Visual Studio Tools -> Visual Studio Command Prom ...
- Best Practices for Web Apps
Mobile Web Best Practices Exceptional Performance Let's make the web faster
- ASP.NET中数据棒图,饼图,柱状图的实现
Web中绘制图形的方法大致有: 1. VML方式:功能强大,但是非常麻烦. 推荐:http://www.elook.net.cn/vml/ 2.使用控件:Dandus, Aspose.chart,Co ...