题目:从1开始连续的整数构成一个字符串,问第n为位对应的字符是谁。
分析:动态规划,dp。统计每个数字对应的前面的子串的长度;
找到给定长度的对应的串的子串,然后取对应数字的n-len(子串)的字符即可。
说明:加油╮(╯▽╰)╭。
#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include <cmath> using namespace std; long long sum[20000002]; char temp[10]; char *itoc(int i) { int t = i,count = 0; while (t) { count ++; t = t/10; } temp[count --] = 0; while (i) { temp[count --] = i%10 + '0'; i /= 10; } return temp; } int bs(int key) { int l = 0,r = 20000000; while (l < r) { int mid = (l+r+1)/2; if (sum[mid] >= key) r = mid-1; else l = mid; } return l; } int main() { sum[0] = 0LL; int add = 1; for (int i = 1; i < 20000001; ++ i) { if (i == 10 || i == 100 || i == 1000 || i == 10000 || i == 100000 || i == 1000000 || i == 10000000) add ++; sum[i] = sum[i-1]+add; } int n,m; while (~scanf("%d",&n)) { m = bs(n); printf("%c\n",itoc(m+1)[n-sum[m]-1]); } return 0; }
时间: 2024-10-10 05:12:14