fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. long long q,k,dig,num,i,cnt,j,p;cin>>q;
  6. while(q--){
  7. dig=1;cnt=0;i=9;num=1;
  8. cin>>k;
  9. //Finding no of digits
  10. while(k>cnt){
  11. cnt+=i*dig++;//9+90*2+900*3
  12. i*=10;
  13. }
  14. p=--dig;
  15. //Finding number just 1 less than than the smallest "dig" digits number
  16. while(p-->1)num*=10;num--;p=dig;cnt=0;i=9;j=1;
  17. //calculating how position we've moved till now(out of k)
  18. while(p-->1){
  19. cnt+=i*j++;
  20. i*=10;
  21. }
  22. //remaining position to move
  23. cnt=k-cnt;
  24. //digit we'll get if we further move remaining position
  25. num+=cnt/dig;
  26. //if cnt%dig==0 exactly last digit of "num" is answer else
  27. //the new "num" is num+1 and the answer will be the "cnt" index's(from left)
  28. //digit of new num
  29. if(cnt%dig!=0){
  30. cnt=cnt%dig;p=dig-cnt-1;i=10;num++;
  31. while(p--)i*=10;
  32. num/=i;
  33. }
  34. cout<<num%10<<'\n';
  35. }
  36. return 0;
  37. }
Success #stdin #stdout 0s 4784KB
stdin
3
7
19
12
stdout
7
4
1