Naxocist

Digit Queries [Brute Force]

Apr 22nd, 2022 (edited)
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.54 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define INF 1e9
  5.  
  6. // find number at index n in "0123456789101112131415..."
  7.  
  8. int main() {
  9.     int n; scanf("%d", &n);
  10.  
  11.     if(n >= 0 && n<=10){
  12.         printf("%d", n-1);
  13.         return 0;
  14.     }
  15.  
  16.     int cnt = 11;
  17.     for (int i = 10; i <= n; i++) // iterative solution
  18.     {
  19.         int k = i, d = 0;
  20.         while(k) k/=10, d++;
  21.         d = pow(10, d-1);
  22.         k = i;
  23.        
  24.         while(d){
  25.             int x = k / d;
  26.             if(cnt == n){
  27.                 printf("%d", x);
  28.                 return 0;
  29.             }
  30.            
  31.             k -= x*d;
  32.             d /= 10;
  33.             cnt++;
  34.         }
  35.     }
  36.    
  37.     return 0;
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment