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. Java自学-集合框架 hashCode原理

    Java hashCode原理 步骤 1 : List查找的低效率 假设在List中存放着无重复名称,没有顺序的2000000个Hero 要把名字叫做"hero 1000000"的 ...

  2. docker centos 镜像中安装python36详解!生成centos+python36的基础镜像

    获取centos镜像docker pull centos:7.4.1708 启动并进入centos的容器docker run -i –t centos /bin/bash下载安装python编译环境依 ...

  3. Javascript获取当前鼠标在元素内的坐标定位

    代码如下: <!doctype html> <html> <head> <meta charset="utf-8"> <tit ...

  4. delphi中json转dataset

    unit uJSONDB; interface uses SysUtils, Classes, Variants, DB, DBClient, SuperObject, Dialogs; type T ...

  5. [题解] LuoguP5666 树的重心

    这个题......确实是CSPNOIP题qwq 感觉猜到一个性质就差不多了,首先,对于一棵树,随便拎一个节点\(rt\)当根节点,那么他的重心一定在\(rt\)的重儿子里,进一步的,可以发现重心一定在 ...

  6. 05 MySQL数据类型的选择与使用

    数据类型的选择     1.CHAR与VARCHAR           存储/检索的方式不同.         CHAR是固定长度,而VARCHAR是可变长度         非SQLMode下,超 ...

  7. mysql8 安装&问题解决

    1.下载:https://dev.mysql.com/downloads/mysql/ 2.安装 1).设置环境变量 MYSQL_HOME D:\env\j2ee\mysql\mysql-8.0.19 ...

  8. HTML学习第二天

    HTML学习第二天 今天学的比较少,有些乱,先只写一个知识点 三种样式表插入方式 外部样式表: <link rel="stylesheet" type="text/ ...

  9. iphone 面试题(转)

    转]iPhone 面试题解答 2011-07-20 0:51 转载自  492437598 最终编辑  492437598 1.main()  {     int a[5]={1,2,3,4,5};  ...

  10. 127个常用的JS代码片段,每段代码花30秒就能看懂(上)

    127个常用的JS代码片段,每段代码花30秒就能看懂(上) JavaScript 是目前最流行的编程语言之一,正如大多数人所说:“如果你想学一门编程语言,请学JavaScript.” FreeCode ...