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. 小程序包大小超过2M的解决方法

    小程序的包被限制在2M以下, 超出的时候点击预览, 发现报错: Error: 代码包大小为 3701 kb,上限为 2048 kb,请删除文件后重试 1. 优化代码, 删除掉不用的代码 2. 图片压缩 ...

  2. 026、Java中改变运算优先级

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  3. 033.SAP上查看IDOC接口,PI接口查不到的日志记录,可能在IDOC接口日志里面

    01. SAP系统发料之后,数据没有传输到条码系统,同事也没有任何bc01的日志,这是就要考虑是不是在IDOC接口了,输入事务代码WE02或者WE05 02.双击查看内容 03.点开就能看到详细内容了 ...

  4. VMWare 禁用vmem虚拟内存文件

    使用 VMWare 虚拟机,虚拟机启动后,会在虚拟机目录下建立一个与虚拟内存大小相同的 .vmem文件 这个文件主要是将虚拟机内存的内容映射到磁盘,以支持在虚拟机的暂停等功能 对所有的虚拟机" ...

  5. 读取多张MNIST图片与利用BaseEstimator基类创建分类器

    读取多张MNIST图片 在读取多张MNIST图片之前,我们先来看下读取单张图片如何实现 每张数字图片大小都为28 * 28的,需要将数据reshape成28 * 28的,采用最近邻插值,如下 def ...

  6. Maven插件方式使用Mybatis Generator

    Mybatis Generator Mybatis Generator简称MBG,可以根据数据库自动生成实体类.单表查询接口及其映射xml文件(也可以选择以注解方式生成). 下面介绍一下以maven插 ...

  7. js数据类型 判断

    1. js数据类型(两种数据类型) 基本数据类型:null undefined number boolean symbol string 引用数据类型: array object null: 空对象 ...

  8. P1051复数乘法

    P1051复数乘法 转跳点:

  9. python——字符输出ASCII码

    总是忘记事,赶紧记下来,Python字符转成ASCII需要用到一个函数ord # 用户输入字符 ch = input("请输入一个字符: ") # 用户输入ASCII码,并将输入的 ...

  10. MongoDB 常用查询语法

    一.查询 find方法 db.collection_name.find(); 查询所有的结果: select * from users; db.users.find(); 指定返回那些列(键): se ...