acm pku 1019 Number Sequence的递归计算方法
来源:互联网 发布:淘宝网秧歌服 编辑:程序博客网 时间:2024/04/30 06:31
Number Sequence 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. 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 Source Tehran 2002, First Iran Nationwide Internet Programming Contest 这道题目其实就是一道数学归纳题,用递归方法可以求得其结果,为此需要如下参数: Len[N]:表示第一次出现数字N(而不是字符串N)时,数字序列的长度。
For example, the first 80 digits of the sequence are as follows:
11212312341234512345612345671234567812345678912345678910123456789101112345678910
Calc[N]:表示数字序列123…N的数字个数。
对这道题目必定有:
假设要求的数字序列的第N个数字idata时,其算法如下:
Step 1: 求出小于等于2147483647的所有Len以及相应的Calc;
Step 2: 寻找Len[i]使得Len[i]<=N<Len[i+1];
若Len[i]=N,则idata=i%10; 算法结束。
否则(Len[i]<N<Len[i+1]), N-=Len[i];
Step 3: 寻找Calc[i]使得Calc[i]<=N<Len[i+1];
若Calc[i]=N,则idata=i%10;算法结束。
否则(Calc[i]<N<Calc[i+1]),N-=Len[i], idata = i+1的第N位数;算法结束。
具体实现如下:
#include "iostream"
#include "math.h"
using namespace std;
const int N = 100000;
unsigned int Len[N];
unsigned int Calc[N];
void LenAndLen(void)
{
int i = 1;
Len[1] = 1;
Calc[1]= 1;
while(Len[i++] < 2147483647)
{
Calc[i] = Calc[i-1] + (int)floor(log10((double)i) + 1.0);
Len[i] = Len[i-1] + Calc[i];
}
}
int SearchData(unsigned int N)
{
int i = 1;
while(Len[i] < N)
{
i++ ;
}
if(N == Len[i])
{
return i%10;
}
else
{
N -= Len[i-1];
i = 1;
while(Calc[i] < N)
{
i++ ;
}
if(N == Calc[i])
{
return i%10;
}
else
{
N -= Calc[i-1];
return i/((int)pow(10., (double)((int)log10((double)(i*10)))-N))%10;
}
}
}
int main(void)
{
unsigned int t, n;
LenAndLen();
cin >> t;
while(t --)
{
cin >> n;
cout << SearchData(n) << endl;
}
return 0;
}
Problem: 1019
User: uestcshe
Memory: 496K
Time: 16MS
Language: C++
Result: Accepted
- acm pku 1019 Number Sequence的递归计算方法
- PKU ACM-1019题 java Number Sequence
- PKU 1019 Number Sequence
- PKU 1019 Number Sequence 问题的解决
- PKU OJ 1019 Number Sequence
- pku 1019 Number Sequence(逐步求精)
- ACM篇:POJ 1019--Number Sequence
- [ACM]Number Sequence
- Sequence Number -acm
- (ACM)Number Sequence
- 杭电ACM OJ 1005 Number Sequence 简单却重要的公式题 完美考察了递归思想
- acm pku 2091 Recaman's Sequence
- Pku acm 2081 Recaman's Sequence
- ACM HDOJ 1711 (Number Sequence)
- HDU ACM 1005 Number Sequence
- ACM:HDU-1005 Number Sequence
- pku acm 1019
- Pku acm 1423 Big Number 解题报告----求n!的位数
- C++指针探讨 (二) 函数指针
- Autotools
- Oracle数据库管理脚本说明
- DWR远程调用
- 一步一步学习CakePHP(一)基本概念
- acm pku 1019 Number Sequence的递归计算方法
- JNI 的多线程
- 一步学习CakePHP(二)controllers
- 程序员应知——我们不是客户
- 顺序栈
- struts.xml文件中能够自动提示
- C++指针探讨 (三) 成员函数指针
- 闲着无事-IopInvalidDeviceRequest
- tcp/ip(十五)