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 这道题不打表会超时 ,以下 超时代码
#include<iostream>
#include<cmath>
using namespace std;
void f(int n)
{
int p=,j=,t;
bool flag;
while(){
flag=false;
for(j=;j<=p;j++){
int i;
for(i=;;i++){
t=pow((double),i);
if(j/t==){
n-=i;
break;
}
}
if(n<=){
int k=i+n;
flag=true;
n=j%(int)(pow((double),i-k+))/pow((double),i-k);
break;
}
}
if(flag==true)break;
p++;
}
cout<<n<<endl;
}
int main()
{
int t;
cin>>t;
while(t--){
int n;
cin>>n;
f(n);
}
//system("pause");
return ;
}

打表后!!!这个代码不好理解

#include<iostream>
#include<cmath>
using namespace std;
unsigned int a[],s[];
void f()
{
int i;
a[]=;
s[]=;
for(i=;i<;i++)
{
a[i]=a[i-]+(int)log10((double)i)+; //记录1至s[i]个数字的位数和
s[i]=s[i-]+a[i]; //一位 记录 1至s[i]个数字
}
}
int main()
{
int t;
int n;
int i;
cin>>t;
f();
while(t--)
{
cin>>n;
i=;
while(s[i]<n) i++;
int pos=n-s[i-];
int tmp=;
for(i=;tmp<pos;i++) //第n个数字在s[i-1]这个数据组中
{
tmp+=(int)log10((double)i)+;
}
int k=tmp-pos; //数字i从低位数的第k+1位
cout<<(i-)/(int)pow(10.0,k)%<<endl;
}
return ;
}

E - Number Sequence(第二季水)的更多相关文章

  1. F - The Fun Number System(第二季水)

    Description In a k bit 2's complement number, where the bits are indexed from 0 to k-1, the weight o ...

  2. D - Counterfeit Dollar(第二季水)

    Description Sally Jones has a dozen Voyageur silver dollars. However, only eleven of the coins are t ...

  3. I - Long Distance Racing(第二季水)

    Description Bessie is training for her next race by running on a path that includes hills so that sh ...

  4. Y - Design T-Shirt(第二季水)

    Description Soon after he decided to design a T-shirt for our Algorithm Board on Free-City BBS, XKA ...

  5. N - Robot Motion(第二季水)

    Description A robot has been programmed to follow the instructions in its path. Instructions for the ...

  6. B - Maya Calendar(第二季水)

    Description During his last sabbatical, professor M. A. Ya made a surprising discovery about the old ...

  7. S - 骨牌铺方格(第二季水)

    Description          在2×n的一个长方形方格中,用一个1× 2的骨牌铺满方格,输入n ,输出铺放方案的总数.         例如n=3时,为2× 3方格,骨牌的铺放方案有三种, ...

  8. R - 一只小蜜蜂...(第二季水)

    Description          有一只经过训练的蜜蜂只能爬向右侧相邻的蜂房,不能反向爬行.请编程计算蜜蜂从蜂房a爬到蜂房b的可能路线数.         其中,蜂房的结构如下所示.     ...

  9. G - Reduced ID Numbers(第二季水)

    Description T. Chur teaches various groups of students at university U. Every U-student has a unique ...

随机推荐

  1. iOS 面试题汇总

    1. Object-c的类可以多重继承么?可以实现多个接口么?Category是什么?重写一个类的方式用继承好还是分类好?为什么? 答: Object-c的类不可以多重继承;可以实现多个接口,通过实现 ...

  2. 新浪授权认证(不用SDK)

    微博开放平台:http://open.weibo.com/ 微博开放接口的调用,如发微博.关注等,都是需要获取用户身份认证的.目前微博开放平台用户身份鉴权主要采用的是OAuth2.0.另外,为了方便开 ...

  3. A Bit Of Knowledge

    iOS推崇使用png格式的图片,说这样不会失帧 imageNamed 和 imageWithContentOfFile的区别 imageNamed会使用系统缓存,对重复加载的图片速度会快一些,效果好. ...

  4. Ubuntu13.04手动安装nvidia显卡驱动

    1. 下载最新版的nVidia驱动,命名为NVIDIA.run. http://www.nvidia.com/page/drivers.html 2.编辑blacklist.conf. sudo ge ...

  5. 基于cygwin构建u-boot(一)环境搭建

    从本文开始,更系统的描述基于cygwin构建u-boot. 之前<痛苦的版本对齐>系列,对于cygwin环境下Sourcery CodeBench,u-boot-1.1.6的构建有侧面的说 ...

  6. haproxy之负载均衡算法

    backend     blance 算法 动态hash的特点     服务器运行时就可进行权重调整,即只需要重新载入配置文件即可,不需要重新启动haproxy     支持慢速启动,即重新恢复的后端 ...

  7. information_schema.collations 学习

    information_schema.collations 表中的每一行对应一个排序规则 1.information_schema.collations 表中学用列: 1.id :排序规则的ID 2. ...

  8. iOS开发之OC篇-响应式编程Reactive Cocoa

    一.Reactive Cocoa 介绍 Reactive Cocoa 是 iOS 开发的一个 "重量级" 框架 高大上的概念:响应式编程 核心概念:信号 Signal 官方网站:h ...

  9. github orgmode

    http://blog.nicky1605.com/application-github-page.html http://dayigu.github.io/WhyUseOrgModeToWriteB ...

  10. cxf2.7.10与Spring3.0.5集成时报错如下

    严重: Error listenerStart 2014-3-29 22:25:20 org.apache.catalina.core.StandardContext start 严重: Contex ...