题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1597
假设:
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
第一个数只有一位,第二个两位,以此类推,我们可以先做一个前缀和,我们可以通过找第几个数字来判断他在第几个式子中,用二分来找,找到下标,取模9+1即可。
#pragma GCC optimize(2) #include<stdio.h> #include<algorithm> #include<iostream> #include<string.h> #include<set> #include<vector> #include<string> #include<queue> #include<math.h> using namespace std; const int maxn = 1e5 + 10; const int inf = 0x3f3f3f3f; typedef long long ll; ll ans[maxn]; int pos[maxn]; int main() { //freopen("C://input.txt", "r", stdin); int k; scanf("%d", &k); for (int i = 1; i <= maxn; i++) { pos[i] = i; } for (int i = 1; i <= maxn; i++) { ans[i] = ans[i - 1] + pos[i]; } for (int i = 1; i <= k; i++) { ll m; scanf("%lld", &m); int l = 1, r = maxn; int mid; while (l <= r) { mid = (l + r) / 2; if (ans[mid] == m) { break; } else if (ans[mid] > m) { r = mid - 1; } else { l = mid + 1; } } printf("%lld\n", (m - ans[l - 1] - 1) % 9 + 1); } return 0; }
hdu 1597 find the nth digit(思维)
最新推荐文章于 2019-08-10 13:10:15 发布