凑平方数 蓝桥真题

题意有问题 明明应该是组间无序 却写成组内无序 坑的一批啊

先全排列 然后将每个排列都分成数份 因为组间无序 所以先排个序再去重

写这道题有两个智障错误 第一是忘开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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值