-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem0062.cpp
59 lines (54 loc) · 1.26 KB
/
problem0062.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
#include <iostream>
#include <unordered_map>
#include <cmath>
std::string sort_digits(long long n);
std::string get_key(int n);
long long first_cube(std::string key);
int main() {
std::cout << first_cube(get_key(5)) << std::endl;
return 0;
}
long long first_cube(std::string key) {
long long cube;
for (int i = 1; i < 10000; ++i) {
cube = pow(i, 3);
if (sort_digits(cube) == key)
return cube;
}
return -1;
}
std::string sort_digits(long long n) {
int digits[10];
for (int i = 0; i < 10; ++i)
digits[i] = 0;
while (n > 0) {
++digits[n%10];
n /= 10;
}
std::string sorted = "";
for (int i = 0; i < 10; ++i) {
while (digits[i] > 0) {
sorted = sorted + (char)(i + '0');
--digits[i];
}
}
return sorted;
}
std::string get_key(int n) {
std::unordered_map<std::string, int> cubes;
std::string key;
long long cube;
for (int i = 1; i < 10000; ++i) {
cube = pow(i, 3);
key = sort_digits(cube);
if (cubes.find(key) == cubes.end()) {
cubes[key] = 1;
} else {
++cubes[key];
if (cubes[key] == n) {
return key;
}
}
}
return "";
}