Number Sequence
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 36013   Accepted: 10409

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

题意是给定了一串有规律的数,问位置i的数是0~9中的哪一个。

自己一直再跟着POJ分类的题目在做,然后很自然地就会总看小优的博客。。。但是觉得这道题没有小优说的那么夸张,多么难。

首先计算每一个数的长度,比如11,n[11]=2。

之后计算从1到n 这一段数的长度,比如1234567891011,所以c[11]=13。

最后计算总体的长度,比如1121231234123451234561234567123456781234567891234567891012345678910111,即s[11]=70

初始化这些结束之后,剩下的就是不断切分,先二分找s数组,之后定位到哪一段,即是c数组的事情,在之后定位到是哪一个数,是n数组的事情,再然后就是这个数的第几位,手动算吧。这样逐级下来,就能得到第几个数是什么。

主要就是做的时候思路清晰,别乱就好。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; int c[52850];
long long s[52850];
int n[52850];
int num; int length(int x)
{
int sum=0;
while (x != 0)
{
x /= 10;
sum++;
}
return sum;
} void init()
{
int i; c[0] = 0;
s[0] = 0; for (i = 1; i <= 52849; i++)
{
n[i] = length(i);
}
for (i = 1; i <= 52849; i++)
{
c[i] = c[i - 1] + n[i];
} for (i = 1; i <= 52849; i++)
{
s[i] = s[i - 1] + c[i];
}
} int main()
{
init(); int Test,left,pos;
int i;
scanf("%d", &Test); while(Test--)
{
scanf("%d", &num); left = lower_bound(s + 1, s + 52845, num) - (s + 1);
pos = num - s[left]; left= lower_bound(c + 1, c + 52845, pos) - (c + 1);
pos = pos - c[left]; left++;
pos = n[left] - pos +1; i = 1;
while (i < pos)
{
left /= 10;
i++;
}
cout << left % 10<<endl;
} return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 1019:Number Sequence 二分查找的更多相关文章

  1. Poj 1019 Number Sequence( 数据分析和操作)

    一.题目大意 有这样一个序列包含S1,S2,S3...SK,每一个Si包括整数1到 i.求在这个序列中给定的整数n为下标的数. 例如,前80位为1121231234123451234561234567 ...

  2. poj 1019 Number Sequence 【组合数学+数字x的位宽函数】

    题目地址:http://poj.org/problem?id=1019 Number Sequence Time Limit: 1000MS   Memory Limit: 10000K Total ...

  3. POJ 1019 Number Sequence

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

  4. POJ 1019 Number Sequence 解读

    这是一个看似简单,其实很难受. 本来我想发挥它的标题轨道基础.没想到反被消遣-_-|||. 看它在个人基础上,良好的数学就干脆点,但由于过于频繁,需求将被纳入全,因此,应该难度4星以上. 方法就是直接 ...

  5. POJ - 1019 Number Sequence (思维)

    https://vjudge.net/problem/POJ-1019 题意 给一串1 12 123 1234 12345 123456 1234567 12345678 123456789 1234 ...

  6. POJ 3273 Monthly Expense二分查找[最小化最大值问题]

    POJ 3273 Monthly Expense二分查找(最大值最小化问题) 题目:Monthly Expense Description Farmer John is an astounding a ...

  7. POJ 3273 Monthly Expense(二分查找+边界条件)

    POJ 3273 Monthly Expense 此题与POJ3258有点类似,一开始把判断条件写错了,wa了两次,二分查找可以有以下两种: ){ mid=(lb+ub)/; if(C(mid)< ...

  8. Subset POJ - 3977(折半枚举+二分查找)

    题目描述 Given a list of N integers with absolute values no larger than 10 15, find a non empty subset o ...

  9. poj 2289 网络流 and 二分查找

    #include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #d ...

随机推荐

  1. 【pwnable.tw】 seethefile

    一开始特别懵的一道题. main函数中一共4个功能,openfile.readfile.writefile.closefile. 其中,在最后退出时有一个明显的溢出,是scanf("%s&q ...

  2. mongodb- 备份和导入备份

    一.使用 mongodump 命令备份数据 mongodump -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -c 表 -o 文件存放路径 参数说明: -h 指明数据库宿主机 ...

  3. 【转】Spark Streaming 实时计算在甜橙金融监控系统中的应用及优化

    系统架构介绍 整个实时监控系统的架构是先由 Flume 收集服务器产生的日志 Log 和前端埋点数据, 然后实时把这些信息发送到 Kafka 分布式发布订阅消息系统,接着由 Spark Streami ...

  4. html5移动端主流适配方案

    1.流式布局(百分比布局)    案例:京东移动端 优点:简单方便,只需要固定高度,宽度自适应: 缺点:大屏幕手机实际显示的不协调. 2.响应式布局 优点:可以节约成本,不用再做专门的web app网 ...

  5. centos6或7查看端口占用及解除占用

    一.查看端口占用 netstat -lnp|grep 要查看的端口号 例如:查看占用端口7000的进程 netstat -lnp|grep 7000 二.清除占用 (1)一次性的清除占用80端口的程序 ...

  6. 如何用naviecat批量创建mysql数据

    1.参考博文:https://blog.csdn.net/lelly52800/article/details/87267096 2.excel要与表结构一致 3.右键,导入向导,选择相应版本,点击“ ...

  7. P1050 螺旋矩阵

    P1050 螺旋矩阵 转跳点:

  8. 小米百元NFC智能神器来了:必成爆款!

    6月14日,小米手环4全渠道首卖,其中电商平台今日10:00开卖,线下小米之家需要顾客持本人身份证限购一台. 首销结束后,米家官微宣布,小米手环4下一轮开售时间在6月18日上午10:00,届时小米手环 ...

  9. 用JS改变embed标签的src属性

    思路: A.先隐藏embed标签 B.清除embed元素 C.为embed重新赋值,加入Html页面中 1.html代码 <object id="forfun" classi ...

  10. 被疯狂吐槽的iPhoneXR屏幕真如传言中的那么差吗?

    在双十一期间,最受果粉关注的电商平台不是天猫,也不是京东,而是拼多多!原因很简单,拼多多疯狂给出iPhone的各种超低价格,直接压制了竞争对手.以iPhone XR为例,依据容量不同,价格分别为558 ...