Question:
Solve:
怎么暴力都行,哪怕选择把数字存成字符串,然后双重循环遍历平方前后的数字都可以
优化点就是1,5,6,0结尾的数字不可能满足题意
Code:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int cnt[10];
bool check(ll x,ll i)
{
//记录原数中的数字个数
while(i){ cnt[i%10]++; i /= 10; }
//判断原数是否出现相同数字
for(int i = 0; i <= 9; i++) if(cnt[i] > 1) return false;
//判断平方后的数字
while(x){
if(cnt[x%10] != 0) return false;
x /= 10;
}
return true;
}
int main()
{
for(ll i = 100002; i <= 999999; i++)
{
//优化
if(i%10==1 || i%10==0 || i%10==5 || i%10==6 || i==203879) continue;
//初始化
memset(cnt,0,sizeof(cnt));
ll temp = i*i;
if(check(temp,i)){ cout <<i; return 0; }
}
}
声明:图片均来源于蓝桥杯官网,以个人刷题整理为目的,如若侵权,请联系删除~