find the nth digit
来源:互联网 发布:highcharts php ajax 编辑:程序博客网 时间:2024/06/04 19:09
find the nth digit
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8801 Accepted Submission(s): 2504
Problem Description
假设:
S1 = 1
S2 = 12
S3 = 123
S4 = 1234
.........
S9 = 123456789
S10 = 1234567891
S11 = 12345678912
............
S18 = 123456789123456789
..................
现在我们把所有的串连接起来
S = 1121231234.......123456789123456789112345678912.........
那么你能告诉我在S串中的第N个数字是多少吗?
S1 = 1
S2 = 12
S3 = 123
S4 = 1234
.........
S9 = 123456789
S10 = 1234567891
S11 = 12345678912
............
S18 = 123456789123456789
..................
现在我们把所有的串连接起来
S = 1121231234.......123456789123456789112345678912.........
那么你能告诉我在S串中的第N个数字是多少吗?
Input
输入首先是一个数字K,代表有K次询问。
接下来的K行每行有一个整数N(1 <= N < 2^31)。
接下来的K行每行有一个整数N(1 <= N < 2^31)。
Output
对于每个N,输出S中第N个对应的数字.
Sample Input
61234510
Sample Output
112124解题思路:先找出第n个数在Si,这通过公式i(i+1)/2=n解得i=(2n+0.25)0.5-0.5,此处需将i向上取整而不是向下取整。则第n个数是在Si 中的第n-Si-1个数 ,再mod 9 即可得结果对于S8以上的数据直接通过查表得结果源代码:#include <stdio.h>#include <math.h>#include <stdlib.h>int main(){ int k; double i; unsigned long j,n; int s[]={0,1,1,2,1,2,3,1,2,3,4,1,2,3,4,5,1,2,3,4,5,6,1,2,3,4,5,6,7, 1,2,3,4,5,6,7,8}; scanf("%d",&k); while(k--) { scanf("%ld",&n); if(n<=36) printf("%d\n",s[n]); else { i=ceil(sqrt(2*n*1.0+0.25)-0.5); j=(unsigned long)i-1; n=n-j*(j+1)/2; // printf("i=%lf\n j=%ld\n n1=%ld\n n2=%ld\n",i,j,j*(j+1)/2,n); if(n%9) printf("%d\n",n%9); else printf("9\n"); } } system("pause"); return 0; }
0 0
- find the nth digit
- find the nth digit
- find the nth digit
- find the nth digit
- find the nth digit
- find the nth digit
- find the nth digit
- find the nth digit
- find the nth digit
- HDU1597 find the nth digit
- find the nth digit(数论)
- hdu find the nth digit
- find the nth digit(math)
- HDU1597 find the nth digit
- HDOJ-1597find the nth digit
- hdu Problem - 1597 find the nth digit
- hdu1597 find the nth digit(规律)
- HDU 1597 find the nth digit
- 恶补web之八:jQuery(2)
- flex 实现图片放大缩小恢复组件
- 查看MSSQL数据库表占用的空间
- 字符串长度及自动截取
- Part 2: Language Guide (The Basics, Basic Operators)
- find the nth digit
- 2014年的流水帐
- C#的百度地图开发(五)IP定位
- 相机标定之(1)相机标定的基本概念以及标定的常用方法
- windows像linux般使用gcc,make等工具
- NYOJ 月老的烦恼(1)
- linux 安装openfire
- JAVA基本数据类型转换 .
- as汇编基础程序设计--函数设计及函数堆栈传参问题