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

4 篇文章 0 订阅
2 篇文章 0 订阅

传送门

题意:给你一个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
*/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值