题目描述
小明发现了一个奇妙的数字。它的平方和立方正好把0~9的10个数字每个用且只用了一次。
你能猜出这个数字是多少吗?
请填写该数字,不要填写任何多余的内容。
思路
本题暴力枚举+判断即可
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
bool st[10]; //记录0~9是否存在
void calc(int x)
{
while (x)
{
st[x % 10] = true;
x /= 10;
}
}
bool judge(int n)
{
int a = n * n, b = n * n * n;
memset(st, 0, sizeof st); //清0(false与0等价)
calc(a), calc(b);
for (int i = 0; i < 10; i++)
if (!st[i]) return false; //只要0~9有一个没有出现就返回false
return true;
}
int main()
{
int n = 9;
while (true)
{
if (judge(n)) break; //满足条件退出循环
n += 1;
}
cout << n << endl;
return 0;
}
结果
69