PS 부수기
Project Euler #32 : Pandigital products 본문
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
|
llong solve(VLL p, int index1, int index2) {
int pow = 1, prod1 = 0, prod2 = 0, prod3 = 0;
for (int i = index1; i >= 0; i--) {
prod1 += pow * p[i];
pow *= 10;
}
pow = 1;
for (int i = index2; i > index1 ; i--) {
prod2 += pow * p[i];
pow *= 10;
}
pow = 1;
for (int i = 8; i > index2; i--) {
prod3 += pow * p[i];
pow *= 10;
}
return (prod1 * prod2 == prod3) ? prod3 : 0;
}
int main() {
llong sum = 0;
set<llong> a;
VLL p = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
do {
for (int i = 0; i < 8; i++) {
for (int j = i + 1; j < 8; j++) {
int x = solve(p, i, j);
if (x != 0 && a.count(x) == 0) {
sum += x;
a.insert(x);
}
}
}
} while (next_permutation(p.begin(), p.end()));
cout << sum;
}
|
cs |
'Project Euler' 카테고리의 다른 글
Project Euler #34 : Digit factorials (0) | 2020.08.08 |
---|---|
Project Euler #33 : Digit cancelling fractions (0) | 2020.08.08 |
Project Euler #31 : Coin sums (0) | 2020.08.08 |
Project Euler #30 : Digit fifth powers (0) | 2020.08.08 |
Project Euler #29 : Distinct powers (0) | 2020.08.08 |
Comments