Question:
题目描述
本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。
小明发现了一个奇妙的数字。它的平方和立方正好把 0 ~ 9 的 10个数字每个用且只用了一次。
你能猜出这个数字是多少吗?
运行限制
- 最大运行时间:1s
- 最大运行内存: 128M
Result: 69
Solve:
看到以后想都不用想直接写,唯一可想的点就是这个数肯定是俩位数,三位数的话会超出平方和与立方和一共九个数的限制
不过我很纳闷为什么蓝桥OJ过不了我的代码,我随便揪个编译器1s之内都能跑出来,准确的说是秒出,大无语事件~
Code:
#include <bits/stdc++.h>
using namespace std;
int cnt[10];
//计数
int Cnt (int x){
bool ans = true;
while(x){
cnt[x % 10]++;
if(cnt[x % 10] > 1){
return -1;
}
x /= 10;
}
}
//检测
bool check(){
for(int i = 0; i <= 9; i++){
if(cnt[i] != 1) return false;
}
return true;
}
int main(void)
{
for(int i = 11; ;i++){
memset(cnt, 0, sizeof(cnt));
if(Cnt(i*i) == -1 || Cnt(i*i*i) == -1) continue;
if(check()){
cout <<i;
return 0;
}
}
return 0;
}
最后附上蓝桥杯汇总链接:蓝桥杯C/C++A组省赛历年真题题解
声明:以个人刷题整理为目的,如若侵权,请联系删除~