-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
660.cpp
55 lines (55 loc) · 1.41 KB
/
660.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
__________________________________________________________________________________________________
class Solution {
public:
int newInteger(int n) {
int res = 0, base = 1;
while (n > 0) {
res += n % 9 * base;
n /= 9;
base *= 10;
}
return res;
}
};
__________________________________________________________________________________________________
class Solution {
public:
int newInteger(int n) {
string res = "";
while (n > 0) {
res = to_string(n % 9) + res;
n /= 9;
}
return stoi(res);
}
};
__________________________________________________________________________________________________
class Solution {
public:
int newInteger(int n) {
long d = 9, pre = 0, cur = 0;
while (true) {
pre = cur;
cur = helper(n + cur, d);
if (cur == pre) break;
}
return n + cur;
}
long helper(long m, long d) {
long res = 0, p = 1, q = 1;
for (long i = m; i >= 10; i /= 10) {
p *= 10;
q *= 9;
}
for (long i = m; i >= d; i %= p, p /= 10, q /= 9) {
long a = i / p;
res += a * (p - q);
if (a == d) {
res += i % p + 1; break;
} else if (a > d) {
res += q;
}
}
return res;
}
};