fork download
  1. #include <iostream>
  2. #include <math.h>
  3. using namespace std;
  4.  
  5. int main() {
  6. long long pos,num=9,d=1,res,sub,pos_f;
  7. cin>>pos;
  8. while(1)
  9. {
  10. sub=num*d;
  11. if(sub<=pos)
  12. pos-=sub;
  13. else
  14. break;
  15. num*=10; d++;
  16. }
  17. res=pow(10,d-1);
  18. res+=(pos/d)-(pos%d==0);
  19. cout<<((pos%d==0)?d:pos%d)<<"th digit of "<<res;
  20. pos_f=d-((pos%d==0)?d:pos%d);
  21. while(pos_f>0)
  22. {
  23. res/=10;
  24. pos_f--;
  25. }
  26. cout<<" which is "<<res%10;
  27. return 0;
  28. }
Success #stdin #stdout 0s 3460KB
stdin
999999999
stdout
9th digit of 123456789 which is 9