Let's write all the positive integer numbers one after another from 11 without any delimiters (i.e. as a single string). It will be the infinite sequence starting with 123456789101112131415161718192021222324252627282930313233343536...
Your task is to print the kk-th digit of this sequence.
Input
The first and only line contains integer kk (1≤k≤100001≤k≤10000) — the position to process (11-based index).
Output
Print the kk-th digit of the resulting infinite sequence.
Examples
input
7
output
7
input
21
output
5
1.to_string();
c++11标准增加了全局函数std::to_string:
string to_string (int val);
string to_string (long val);
string to_string (long long val);
string to_string (unsigned val);
string to_string (unsigned long val);
string to_string (unsigned long long val);
string to_string (float val);
string to_string (double val);
string to_string (long double val);
代码示例:(使用编译器vs2013)
#include<iostream> #include <string> //std::string,std::string #include<stdlib.h> using namespace std; int main() { int num; string str; cin >> num; str = to_string(num); cout << str; system("pause"); return 0; }
那么对于本题而言,for循环遍历,把出现的每一个数字转化为sting类型,然后再加入到sting变量中,就OK了。
#include<algorithm>
#include<iostream>
#include<string.h>
#include<cstdio>
#include<string>
#include<stdlib.h>
#define P pair<long long,long long>
#define ll long long
using namespace std;
int main()
{
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);//加速输入输出流
string num="";
int numm;
cin>>numm;
for(int i=1;i<=numm;++i)
{
num+=to_string(i);
}
cout<<num[numm-1]<<endl;
return 0;
}