更新:17.5.23 去年写的帖子,好几个评论以为之前做的是错的,刚比完赛就知道错在哪里了,后面的返回类型只要改为LL就好了,之前是因为越界原因,蓝桥杯尤其注意这些小细节,其他没什么含量,直接上暴力。。。
最倒霉的一次,我直接也是醉了,想在想起来都悔死了,263,这个数字我绝对不会忘掉,一等奖一步之遥,刚考完的时候还在想怎么会这样,最不能确定的题目就是这道了,考试的时候感觉挺简单的,之后聊的时候甚至有点纳闷,怎么还都最不出来呢,结果最后自己却是错掉了,如果不会也就算了,但是全部结果只因为一个LL,两个字母就决定了最终的结果,也是悔死了。。。
但是决赛已经结束,ACM省赛全力以赴吧。。。
8个人,额,就8个人 。 。 。 。 。 。 。 。 。。 。 。 。。 。 。 。。 。。 。 。 。。 。 。 。。 。。 。 。 。。 。。 。 。
正确代码:
#include <iostream>
#include <fstream>
#include <cstring>
#define LL long long
using namespace std;
bool mark[100]; ///标记哪些数字出现过
LL res[100],sum=0; ///res保存结果
bool tmpmark[10]; ///临时标记
bool judge(LL x){ ///判断这个数是否是出现过
memset(tmpmark,0,sizeof(tmpmark));
if(x==0){
if(mark[0] == 1)
return false;
return true;
}
while(x!=0){
if(mark[x%10] || tmpmark[x%10])
return false;
tmpmark[x%10] = 1;
x/=10;
}
return true;
}
int update(LL x,int coun){///用于修改哪些数字已经出现过 ///就错了这个地方的一个LL,两个字母啊啊啊啊啊啊啊 啊啊啊啊啊啊 啊啊 啊啊啊啊 啊啊 啊啊啊啊 啊啊啊啊 啊 啊啊 啊啊啊
if(x==0){
mark[x] = 1;
coun++;
return coun;
}
while(x!=0){
mark[x%10] = 1;
x/=10;
coun++;
}
return coun;
}
void reupdate(LL x){ ///用于还原上一个标记状态
if(x==0){
mark[x] = 0;
return ;
}
while(x!=0){
mark[x%10] = 0;
x/=10;
}
}
void dfs(int coun,int rescoun,LL last){
if(coun == 10){
for(int i=0;i<rescoun;i++)
cout<<res[i]<<' ';
cout<<">>>"<<sum<<endl;
sum++;
return ;
}
for(LL i=last;i<=100000;i++){
if(judge(i*i)){ ///判断是否出现了该数里面含有的数字
int tmpcoun = update(i*i,coun); ///更新新状态(包含i*i)
res[rescoun] = i*i;
dfs(tmpcoun,rescoun+1,i+1);
reupdate(i*i); ///还原上一状态
}
}
}
int main (){
memset(mark,0,sizeof(0));
dfs(0,0,0);
cout<<sum;
return 0;
}