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(大数处理)的更多相关文章

  1. POJ1019 Number Sequence

    Number Sequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 36256   Accepted: 10461 ...

  2. HDU 1005 Number Sequence

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  3. POJ 1019 Number Sequence

    找规律,先找属于第几个循环,再找属于第几个数的第几位...... Number Sequence Time Limit: 1000MS Memory Limit: 10000K Total Submi ...

  4. HDOJ 1711 Number Sequence

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  5. Number Sequence

    Number Sequence   A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) ...

  6. [AX]AX2012 Number sequence framework :(三)再谈Number sequence

    AX2012的number sequence framework中引入了两个Scope和segment两个概念,它们的具体作用从下面序列的例子说起. 法国/中国的法律要求财务凭证的Journal nu ...

  7. KMP - HDU 1711 Number Sequence

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. hdu 1005:Number Sequence(水题)

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  9. 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 ...

随机推荐

  1. STL之multimap

    参见http://www.cplusplus.com/reference/map/multimap/ 多重映射multimap和map映射很相似,但是multimap允许重复的关键字,这使得multi ...

  2. JAVA开发CHECK STYLE

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE module PUBLIC "-/ ...

  3. 【Validate Binary Search Tree】cpp

    题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...

  4. 【Symmetric Tree】cpp

    题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...

  5. Oracle窗口函数显示想要的行数

    Oracle中支持窗口函数ROW_NUMBER(),其用法和 MSSQLServer2005中相同,比如我们 执行下面的 SQL语句: SELECT * FROM ( SELECT ROW_NUMBE ...

  6. WKWebView 与 UIWebView

    apple和google为webkit该浏览器引擎的发扬光大做出了重要贡献,在WWDC 2014发布会上发布iOS 8中,apple公布了WebKit框架,这意味着OSX和IOS开发者将共用同样的开发 ...

  7. bzoj 3232 01分数规划+最大权封闭子图判定

    我们的目标是使v/c最小化,所以构造函数g(x)=v-x*c,那么 二分一个X,判断当时的v-x*c的值是多少,然后根据g(x)函数的 单调递减性来二分,判断,直到g(x)=0的时候当前的X就是答案. ...

  8. Hdu 1507 Uncle Tom's Inherited Land* 分类: Brush Mode 2014-07-30 09:28 112人阅读 评论(0) 收藏

    Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  9. JavaScript在IE6,IE7下报错'expected identifier, string or number'

    问题: 代码在Forefox和IE8下工作正常,但是在IE6下报错: expected identifier, string or number 假如变量options有多个选项,那么我们可以用逗号分 ...

  10. Python中__init__方法/__name__系统变量讲解

    __init__方法在类的一个对象被建立时,马上运行.这个方法可以用来对你的对象做一些你希望的初始化. 代码例子 test.py#!/usr/bin/python# Filename: class_i ...