欧拉计划第32题
作者:
brivia
,
2024-02-25 17:25:29
,
所有人可见
,
阅读 38
/*
欧拉计划第32题
乘数、被乘数、积为1~9的全排列
*/
#include<bits/stdc++.h>
using namespace std;
bool check(int a, int b, int c)
{
//(a, b, c) 是否满足条件
int st[10];
memset(st, 0, sizeof st);
while (a)
{
st[a % 10] ++;
a /= 10;
}
while (b)
{
st[b % 10] ++;
b /= 10;
}
while (c)
{
st[c % 10] ++;
c /= 10;
}
if (st[0]) return false;
for (int i = 1; i <= 9; ++ i)
if (st[i] != 1) return false;
return true;
}
int main(void)
{
set<int> ans;
/*
不妨假设 a < b
则 a 不能为三位数, 否则 a * b > 10000 五位数, 累计超过9位
所以 a < 100.
b 不能小于100, 否则 a * b < 99 & 99 < 10000,累计只有8位。
所以a < 100, b > 100
*/
for (int i = 2; i <= 99; ++ i)
for (int j = 100; j <= 9999; ++ j)
{
int k = i * j;
if (check(i, j, k))
{
cout << i << " " << j << " " << k << endl;
ans.insert(k);
}
}
int sum = 0;
for (auto e: ans) sum += e;
cout << ans.size() << endl;
cout << sum << endl;
return 0;
}