题目:

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

题意分析:

把这些数,根据构造它们的那个数划分成 1; 1,2; 1,2,3;……

然后分析一个数字的位数是怎么确定的,其实,因为数是十进制,可以直接取以10为底的对数+1.

$log_{10}(N)+1$

在写程序的时候,一定要注意N要用double类型。然后我们可以递推求出所有可能的小于MAXN的数,最后一组数就是31268,这个数可以通过写个测试程序测试一下就出来了。

然后就可以确定输入的数字在整个序列中的哪个数字组中,接下来就是确定在这个数字组中的那个数中。

根据序列递增,然后利用求位数的公式,可以直接找到这个数。然后根据N的具体位置,取余取出来即可。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long LL;
const LL MAXN = 2147483647;
const int Msize = 32000;
LL Sum[Msize], a[Msize]; void getS()
{
Sum[1] = a[1] = 1;
int i;
for(i = 2; i < Msize; i++)
{
a[i] = a[i-1] + (int)log10((double)i)+1;
Sum[i] = Sum[i-1] + a[i];
}
return;
} LL Pow(LL a, LL b)
{
LL ans = 1;
while(b)
{
if(b&1)
ans = ans*a;
b>>=1;
a = a*a;
}
return ans;
} int main()
{
getS();
LL N;
int T;
scanf("%d", &T);
while(T--)
{
scanf("%I64d", &N);
int i = 1, j, len, pos;
while(N > Sum[i])
{
i++;
}
//第N位再i-1所代表的数字区
pos = N - Sum[i-1];
len = 0;
for(j = 1; len < pos ; j++)
{
len += (int)log10(j*1.0) + 1;
}
//N为在i-1数字区中的第j-1个数中,len为这个数字的最后一位的位置
int ans = (j-1)/Pow(10, len-pos)%10;
printf("%d\n", ans); }
return 0;
}

  

POJ_1019 Number Sequence 【递推】的更多相关文章

  1. HDU 5860 Death Sequence(递推)

    HDU 5860 Death Sequence(递推) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5860 Description You ...

  2. hdu-5496 Beauty of Sequence(递推)

    题目链接: Beauty of Sequence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java ...

  3. HDU 5950 Recursive sequence 递推转矩阵

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  4. hdu 5860 Death Sequence(递推+脑洞)

    Problem Description You may heard of the Joseph Problem, the story comes from a Jewish historian liv ...

  5. hdu 5950 Recursive sequence 递推式 矩阵快速幂

    题目链接 题意 给定\(c_0,c_1,求c_n(c_0,c_1,n\lt 2^{31})\),递推公式为 \[c_i=c_{i-1}+2c_{i-2}+i^4\] 思路 参考 将递推式改写\[\be ...

  6. luogu P1216 [IOI1994][USACO1.5]数字三角形 Number Triangles (递推)

    链接:https://www.luogu.org/problemnew/show/P1216 题面: 题目描述 观察下面的数字金字塔. 写一个程序来查找从最高点到底部任意处结束的路径,使路径经过数字的 ...

  7. bzoj 2656 [Zjoi2012]数列(sequence) 递推+高精度

    2656: [Zjoi2012]数列(sequence) Time Limit: 2 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Descri ...

  8. hdu 4055 Number String(递推DP)

    给一个只含‘I','D','?'三种字符的字符串,I表示当前数字大于前面的数字,D表示当前的数字小于前面一位的数字,?表示当前位既可以小于又可以大于. 问1~n的排列中有多少个满足该字符串. http ...

  9. 递推:Number Sequence(mod找规律)

    解题心得: 1.对于数据很大,很可怕,不可能用常规手段算出最后的值在进行mod的时候,可以思考找规律. 2.找规律时不必用手算(我傻,用手算了好久).直接先找前100项进行mod打一个表出来,直接看就 ...

随机推荐

  1. Python父类调用子类

    首先说明,上面的标题其实是不正确的,Python是一门解释型.动态数据类型的高级语言,运行时,动态判断调用对象,其实还是子类自己在调用自己的方法或属性. 举个例子(copy过来的):SocketSer ...

  2. sudo apt install libreadline-dev Reading package lists... Error!

    luo@luo-ThinkPad-W540:~$ luo@luo-ThinkPad-W540:~$ luo@luo-ThinkPad-W540:~$ luo@luo-ThinkPad-W540:~$ ...

  3. Entity Framework 6.0 Tutorials(6):Transaction support

    Transaction support: Entity Framework by default wraps Insert, Update or Delete operation in a trans ...

  4. eclipse——Maven插件创建java工程

    目录结构如下 注意默认JDK为1.5 更改默认JDK  方式一 右键工程 选中JRE1.5 Remove 双击JRE System Library 点击Finish 更改完成 方式二 配置maven ...

  5. Monkey测试异常信息解读

    查看包名 1.cmd 下面输入 adb locat > D:\test.txt 2.ctrl+c 停掉刚刚 1 运行的进程 3.打开test.txt文件--搜索  Displayed  对应的内 ...

  6. App测试从入门到精通之安装、卸载和运行测试

    关于手机App测试需要说的点有很多.目前市场上主要的APP测试主要是针对的是安卓.和苹果两大主流操作系统.主要考虑的就是功能性.兼容性.稳定性.性能测试等.我们看下App的安装和卸载有哪些常用的场景: ...

  7. 二度Xml<2>

    一下介绍xml的基本操作,添加xml新节点: 其他方法在前一篇日记中有详细讲解,请详见:http://www.cnblogs.com/fjsnail/archive/2012/10/20/273212 ...

  8. 软件工程:Java实现WC.exe基本功能

    项目相关要求 GitHub地址:https://github.com/3216004716/WC 实现一个统计程序,它能正确统计程序文件中的字符数.单词数.行数,以及还具备其他扩展功能,并能够快速地处 ...

  9. android 多点触控

    多点触控 1.多点触控从字面意思讲就是你用大于等于2根的手指触摸子啊手机屏幕上. Android中监听触摸事件是onTouchEvent方法,它的参数为MotionEvent,下面列举MotionEv ...

  10. 服务器控件数据回发实现IPostBackDataHandler需注意的

    我写的服务器控件(未完,模型如此) using System; using System.Collections.Generic; using System.Collections.Specializ ...