Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 34343 | Accepted: 9860 |
Description
For example, the first 80 digits of the sequence are as follows:
11212312341234512345612345671234567812345678912345678910123456789101112345678910
Input
Output
Sample Input
2 8 3
Sample Output
2 2
Source
大致题意:
有一串数字串,其规律为
1 12 123 1234 12345 123456 1234567 12345678 123456789 12345678910 1234567891011 123456789101112······k
输入位置n,计算这一串数字第n位是什么数字,注意是数字,不是数!例如12345678910的第10位是1,而不是10,第11位是0,也不是10。总之多位的数在序列中要被拆分为几位数字,一个数字对应一位。
注意:2147483647刚好是int的正整数最大极限值( ),所以对于n用int定义就足矣。但是s[31268]存在超过2147483647的位数,因此要用unsigned 或long 之类的去定义s[]。-----我用的unsigned int;
解析: 在我A掉这道题后,参看了别的大牛的博客,其他人都用了很强的数学技巧,而我却没有。到也不这么认为。
先分组 1, 12, 123 , 1234,12345,。。。K 为一组,利用一个a数组记录 该组有多少位
打出最大的[2147483647]分组,里面的值,在这些组中前缀都是一样的,所以我们就打出最大的一组。
题目给一个n,累加数组 a[],找到所在数组的前一个,输出 在第二个表的位置。
具体见代码。。个人认为挺简单的。。
/* 整个世界也填满不了十八岁男孩子的雄心和梦 */
/* By---宇 */
#include <queue>
#include <stack>
#include <math.h>
#include <vector>
#include <limits.h>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <functional>
#define N 31268
#define mem(a) memset(a,0,sizeof(a));
#define mem_1(a) memset(a,-1,sizeof(a));
using namespace std;
unsigned int a[N+10]; //分组
unsigned int ans[N*5+10]; //31268的表
unsigned int f[5]= {1,10,100,1000,10000};
void Init() //模拟分组 + 打出第31268的表
{
unsigned int t,i,j;
/*******模拟分组 把1看做第一组,12看做第2组,123看做第3组 等等*******/
for(i=1; i<=31268; i++)
{
t = i;
if(t<=9) a[i] = t;
else if(t<=99) a[i] = (t-9)*2+9;
else if(t<=999) a[i] = (t-99)*3+90*2+9;
else if(t<=9999) a[i] = (t-999)*4+900*3+90*2+9;
else a[i] = (t-9999)*5+9000*4+900*3+90*2+9;
}
/*****************打出31268的表****************/
unsigned int cnt = 1,flag;
for(i=1; i<=31268; i++)
{
flag = 0;
for(j=5; j>=1; j--)
{
t = i/f[j-1];
t = t%10;
if(t||flag)
{
ans[cnt++] = t;
flag = 1;
}
}
}
}
int main()
{
unsigned int T,i;
long long n;
Init();
cin >> T;
while(T--)
{
cin >> n;
long long sum = 0;
for(i=1;; i++) //寻找在第几个分组的前一组
{
sum+=a[i];
if(sum>=n)break;
}
n = n-(sum-a[i]); //找到该分组的第几位
cout << ans[n]<< endl;; //从表里输出该位
}
return 0;
}