湊平方數 藍橋真題

題意有問題 明明應該是組間無序 卻寫成組內無序 坑的一批啊

先全排列 然後將每個排列都分成數份 因爲組間無序 所以先排個序再去重

寫這道題有兩個智障錯誤 第一是忘開longlong 第二是排序時直接對dfs用的序列排序。。

還有注意遇到當前位置是0時要特殊處理

 

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

map <string,bool> mp;
ll ary[10],pre[10],gou[10];
int ans;

bool judge(ll val)
{
    ll tmp;
    tmp=sqrt(val);
    return tmp*tmp==val;
}

string solve(ll val)
{
    string res;
    if(val==0) return "0";
    res="";
    while(val>0){
        res+='0'+val%10;
        val/=10;
    }
    return res;
}

void dfs(int cur,int cnt)
{
    string s;
    ll tmp;
    int i;
    if(cur==10){
        //sort(pre,pre+cnt);
        for(i=0;i<cnt;i++) gou[i]=pre[i];
        sort(gou,gou+cnt);
        s="";
        for(i=0;i<cnt;i++){
            s+=solve(gou[i]);
            s+=".";
        }
        if(!mp[s]){
            mp[s]=1;
            ans++;
        }
        return;
    }
    if(ary[cur]==0){
        pre[cnt]=0;
        dfs(cur+1,cnt+1);
    }
    else{
        tmp=0;
        for(i=cur;i<10;i++){
            tmp=10ll*tmp+ary[i];
            if(judge(tmp)){
                pre[cnt]=tmp;
                dfs(i+1,cnt+1);
            }
        }
    }
}

int main()
{
    int i;
    for(i=0;i<10;i++) ary[i]=i;
    do{
        dfs(0,0);
    }
    while(next_permutation(ary,ary+10));
    printf("%d\n",ans);
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章