-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathmain.cpp
77 lines (64 loc) · 1.47 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
class Solution {
public:
int findNthDigit(int n) {
long remain = n;
long start = 1;
int d = 1;
while (remain > start * d * 9) {
remain -= start * d * 9;
start *= 10;
d++;
}
int curr = start + (remain - 1) / d ;
int remove_digit = (d - remain % d) %d ;
// cout << curr << "\t" << remove_digit << endl;
while (remove_digit != 0) {
curr = curr / 10;
remove_digit--;
}
return curr % 10;
}
};
int main() {
Solution sol;
int n;
int result;
n = 3;
result = sol.findNthDigit(n);
cout << "n=" << n << "\t" << result << endl;
n = 10;
result = sol.findNthDigit(n);
cout << "n=" << n << "\t" << result << endl;
n = 11;
result = sol.findNthDigit(n);
cout << "n=" << n << "\t" << result << endl;
n = 12;
result = sol.findNthDigit(n);
cout << "n=" << n << "\t" << result << endl;
n = 13;
result = sol.findNthDigit(n);
cout << "n=" << n << "\t" << result << endl;
n = 14;
result = sol.findNthDigit(n);
cout << "n=" << n << "\t" << result << endl;
n = 15;
result = sol.findNthDigit(n);
cout << "n=" << n << "\t" << result << endl;
n = 16;
result = sol.findNthDigit(n);
cout << "n=" << n << "\t" << result << endl;
return 0;
}