Codeforces_961C_Make a Square(枚举状态or深搜)

版权声明: https://blog.csdn.net/yyy_3y/article/details/79965954

传送门

题意:给你一个int范围以内的数字,判断需要删除多少位,能变成一个平方数。不能输出-1。
思路:题目还是非常裸的,只需要枚举或者深搜就行。判断这个数是不是平方数,然后看删除了多少位,维护一个最小值就可以啦。
这道题有些坑点:比如如果用int读入的话,会导致你的末尾的0无法统计。所以用string读再合适不过了。还有就是要注意前导0.

#include<bits/stdc++.h>
#define debug(a) cout << #a << " " << a << endl
#define LL long long
#define ull unsigned long long
#define PI acos(-1.0)
#define eps 1e-6
const int N=1e5+7;
using namespace std;
int a[N];
char s[N];
int main ()
{
    //yyy_3y
    //freopen("1.in","r",stdin);
    scanf("%s",s);
    int n;
    int len=strlen(s);
    for(int i=0;i<strlen(s);i++) a[i]=s[i]-'0',n=n*10+s[i]-'0';
    LL mi_depth=100;
    for(LL i=1;i<(1<<len);i++){
        LL depth=0;
        LL tmp=0;
        for(LL j=0;j<len;j++){
            if(i&(1<<(len-1-j)) ) {
                if(!a[j] && !depth) continue;
                depth++,tmp=tmp*10+a[j];
            }
        }
        LL tmpt=sqrt(tmp);
        if(tmp && tmpt*tmpt==tmp && mi_depth>len-depth){
            mi_depth=len-depth;
        }
    }
    LL tmpt=sqrt(n);
    if(tmpt*tmpt==n) {
        printf("0\n");
        return 0;
    }
    if(mi_depth==100) printf("-1\n");
    else printf("%lld\n",mi_depth);
    return 0;
}
/*
2000000000
*/

猜你喜欢

转载自blog.csdn.net/yyy_3y/article/details/79965954