您的位置:首页 > 产品设计 > UI/UE

Number Sequence 无算法,靠思想 数学题

2013-10-23 18:53 323 查看
[align=left]Problem Description[/align]
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

[align=left]Input[/align]
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)

[align=left]Output[/align]
There should be one output line per test case containing the digit located in the position i.

[align=left]Sample Input[/align]

2
8
3

[align=left]Sample Output[/align]

2
2

***************************************************************************************************************************
***************************************************************************************************************************

/*
一个数的位数可用log10((double)value)+1表示
a[]表示子串的长度,b[]表示到第i个子串时的总长度
*/
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<queue>
#include<cstdlib>
#define maxn  32000
using namespace std;
typedef  long  LL;
unsigned int a[maxn],b[maxn];
unsigned int n,m,j,k;
unsigned int cas;
int main()
{
a[0]=0;
b[0]=0;
a[1]=1;
b[1]=1;
LL i;
for(i=2;i<maxn;i++)
{
a[i]=a[i-1]+log10((double)i)+1;
b[i]=b[i-1]+a[i];
}
scanf("%u",&cas);
while(cas--)
{
scanf("%u",&n);
for(j=0;j<maxn;j++)
{
if(n<=b[j])
break;
}
unsigned int remain=n-b[j-1];
unsigned int sum=0,value;
for(value=1;value<maxn;value++)
{
sum+=(1+log10((double)value));
if(sum>=remain)
break;
}
unsigned int value_bit=1+log10((double)value);
unsigned int position=remain-(sum-value_bit);
unsigned int temp1=value;
char* str = (char*)malloc(10*sizeof(char));
unsigned int num=0;
while(temp1)
{
unsigned int temp=temp1%10;
str[num++]=temp+'0';
temp1/=10;
}
str[num]='\0';
if(position==value_bit)
printf("%c\n",str[0]);
else
printf("%c\n",str[position-1]);

}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: