A. Digits Sequence (Easy Edition)
time limit per test
1 secondmemory limit per test
256 megabytesinput
standard inputoutput
standard outputLet's write all the positive integer numbers one after another from without any delimiters (i.e. as a single string). It will be the infinite sequence starting with 123456789101112131415161718192021222324252627282930313233343536...
Your task is to print the -th digit of this sequence.
Input
The first and only line contains integer () — the position to process (-based index).
Output
Print the -th digit of the resulting infinite sequence.
Examples
input
Copy
7
output
Copy
7
input
Copy
21
output
Copy
5
Solution
Explanation-
In the above question, we have to find the kth digit when all numbers are written in a string without spaces. So for this we will simply make a vector and we will add all the numbers to it by converting current number into string, and then simply find the k-1th number in the vector.
#include<bits/stdc++.h>using namespace std;#define vv vector<int> v;int main(){#ifndef ONLINE_JUDGE// for getting input from input.txtfreopen("input.txt", "r", stdin);// for writing output to output.txtfreopen("output.txt", "w", stdout);#endif//taking the input kint k;cin>>k;//making a vector to store all the digits into char formvector<char>v;// t for converting the t into string and then storing all the digits in vectorint t=1;//making start so that we can count the lenght of current number (t)int start=0;for(int i=0; i<k; i++){string s=to_string(t);start=0;while(start<s.length()){v.push_back(s[start]);// cout<<s[start]<<" ";start++;}t++;}cout<<v[k-1];return 0;}
0 Comments
If you have any doubts/suggestion/any query or want to improve this article, you can comment down below and let me know. Will reply to you soon.