-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdigitAtIndex.cpp
56 lines (43 loc) · 980 Bytes
/
digitAtIndex.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
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int countOfIntegers(int digits) {
if( digits == 1) return 10;
int count = (int)pow(10, digits-1);
return 9*count;
}
int beginNumber(int digits) {
if( digits == 1) {
return 0;
}
return (int)pow(10, digits-1);
}
int digitAtIndex( int num, int digits) {
int number = beginNumber(digits) + num / digits;
int index_right = digits - num % digits;
for( int i = 1; i < index_right; ++i) {
number /= 10;
}
return number %10;
}
int getDigitAtIndex( int num) {
if( num < 0) return -1;
int digits = 1;
while(true) {
int tmp = countOfIntegers(digits);
if( num < tmp * digits) {
return digitAtIndex(num, digits);
}else {
num -= tmp * digits;
}
++digits;
}
return -1;
}
int main(void) {
int num = 10;
int ans = getDigitAtIndex(num);
cout << "the digit at index[" << num << "] is " << ans << endl;
return 0;
}