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
***************************************************************************************************************************
***************************************************************************************************************************
View Code
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
相关文章推荐
- C/C++面试之算法系列--如何利用数学思想解1/2/5组合问题
- C/C++面试之算法系列--如何利用数学思想解1/2/5组合问题
- 编程语言,算法,思想,数学建模,数学思想
- 数学思想和算法
- 算法中的数学思想
- 算法分析的数学基础
- 图的点着色、区间着色问题及其应用(基于贪心思想的DFS回溯法求点着色问题和区间着色算法求解任务调度问题)
- 数学排列算法---P(N,M)
- 数学建模学习笔记(7)用导数的思想建模
- 【算法思想】循环移动一个数组
- 创建数学算法工具类事例
- 不具备数学和算法知识能否成为优秀的开发人员(2)
- 游戏开发中的数学和物理算法(15):矩阵的加减法
- 游戏开发中的数学和物理算法(4):抛物线
- [数据结构]一些常见的算法思想
- BP神经网络的数学原理及其算法实现
- bresenhan算法数学推导
- MP算法和OMP算法及其思想
- 数学建模算法 一 简述(3)规划模型-整数规划
- MATLAB实现构造Euler环游图的算法——数学建模