题目:

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;
}

  

最新文章

  1. 在Eclipse中生成接口的JUnit测试类
  2. ios开发中如何实现软件版本更新
  3. ios开发之C语言第一天
  4. ROBOTS.TXT屏蔽笔记、代码、示例大全
  5. Office 2010
  6. C#小性能知识
  7. Windows Azure 社区新闻综述(#71 版)
  8. No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=x86_64, VALID_ARCHS=armv7 armv7s)
  9. CentOS克隆机器步骤,图文教程
  10. Hadoop3.0 WordCount测试一直Accept 状态,Nodes of the cluster 页面node列表个数为0
  11. commons-httpclient 实现get和post请求
  12. readlink 获取进程的绝对路径
  13. River Problem HDU - 3947(公式建边)
  14. Tomcat与Web应用
  15. python第一天,编写用户接口
  16. django rest framework通用view
  17. Kotlin 范型约束
  18. 使用ES6的reduce函数,根据key去重
  19. zookeeper&amp;acticemq&amp;redis&amp;tomcat安装
  20. 提取nmap扫描出来的xml文件

热门文章

  1. id 和 instancetype 方法的区别
  2. GPS通讯协议协议(NMEA0183)
  3. 杀毒软件 avg
  4. csv、txt读写及模式介绍
  5. Cannot resolve the collation conflict between &quot;Chinese_PRC_CI_AS&quot; and &quot;SQL_L及由于排序规则不同导致查询结果为空的问题
  6. [GO]随机数的使用
  7. Oracle 写存储过程的一个模板还有一些基本的知识点
  8. exe文件停止运行的情况
  9. Django项目运行时出现self.status.split(&#39; &#39;,1)[0], self.bytes_sent,ConnectionAbortedError: [WinError 10053] 你的主机中的软件中止了一个已建立的连接。
  10. Android Butterknife框架 注解攻略