hdu 1597 find the nth digit
来源:互联网 发布:莱芜市宽客网络 编辑:程序博客网 时间:2024/06/01 13:43
Problem Description
假设:
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)。
Output
对于每个N,输出S中第N个对应的数字.
Sample Input
6
1
2
3
4
5
10
Sample Output
1
1
2
1
2
4
Author
8600
思路:现在我们根据题意可知道每个字符串的长度,那么我们要找长度为N的字符串的最后一位是多少,我们就可以先求出我们用了多少个完整字符串,我们又得知每个字符串的长度是有规律的,那么我们就可以很快的求出最后一位的数是多少了。
AC代码:
#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>using namespace std;int main(){ int sum; int k; scanf("%d",&k); while(k--) { sum = 0; int n, i = 0; scanf("%d",&n); while(sum < n) ///首先判断长度为n至少需要多少个完整的字符串 { i++; sum+=i; } if(sum == n) ///如果相等 那么第i个字符串的最后一位就是答案 { int m = i % 9; if(m==0) printf("9\n"); else printf("%d\n",m); } else if(sum > n) ///如果大于 说明第i个字符串是拆开的 那么我们就求出第i个字符串到底用了几个, 最后一个就是答案 { sum-=i; int p = n - sum; int q = p % 9; if(q==0) printf("9\n"); else printf("%d\n",q); } } return 0;}
1 0
- hdu Problem - 1597 find the nth digit
- HDU 1597 find the nth digit
- hdu 1597 find the nth digit
- Hdu 1597 find the nth digit
- hdu 1597 find the nth digit
- hdu 1597 find the nth digit
- HDU 1597 find the nth digit
- HDU 1597:find the nth digit
- hdu 1597 find the nth digit
- hdu find the nth digit(1597)
- hdu 1597 find the nth digit
- hdu 1597 数学题 find the nth digit
- HDU 1597 find the nth digit
- HDU 1597 find the nth digit
- HDU 1597 find the nth digit 水题
- HDU 1597:find the nth digit【规律】
- HDU 1597 find the nth digit
- HDU 1597 find the nth digit
- 轮播也可以这么简单
- 【基于SSH框架的个人博客系统01】Eclipse搭建SSH框架详细流程
- 顺时针打印矩阵
- 关于rand和srand函数使用的一点心得
- 当鼠标离开输入框判断数据是否存在
- hdu 1597 find the nth digit
- jbpm分支decision节点判断跳转
- 父fragment刷新子fragment无反应问题解决方法
- viewer.js插件的使用
- 我可能下了一个假的Wi-Fi万能钥匙:竟然有1387个山寨Wi-Fi万能钥匙!
- [网络电话]Android CSipSimple开发实例
- Python-Numpy的tile函数用法
- C语言基础部分
- 【2017/4/13】ORA-27125: unable to create shared memory segment