2016年第七届蓝桥杯C/C++程序设计本科B组决赛

2.答案300

刁丝卫代码,比赛时long long写成int,结果成了263。。。一等擦肩而过。。。

#include <iostream>
#include <fstream>
#include <cstring>
#define LL long long
using namespace std;
bool mark[100];
LL res[100],sum=0;
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){
  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);
        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;
}
View Code
原文地址:https://www.cnblogs.com/gongpixin/p/5550642.html