Codeforce962B(二进制枚举暴力)

Make a Square

传送门

题意:给你一个数字(没有前导零),要求你删除一些数字,使得这个数字(不能有前导零)的平方根为整数。没有则输出-1.
思路:数字不大,二进制枚举每一种组合情况。

二进制枚举实现传送门

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>
#include <functional>
#define inf 1000000000
using namespace std;
typedef long long ll;
const int MAXN=1e9+10;
const int MAX=1e2+10;
const double eps=1e-6;

string s;
int vis[MAX];

ll getnum(string t){
    int cnt=0,len=t.size()-1;
    ll res=0;
    while(len+1){
        double temp=1;
        for(int i=1;i<=cnt;i++) temp*=10;
        res+=temp*(t[len]-'0');
        len--;cnt++;
    }
    return res;
}

int main(){
    #ifdef ONLINE_JUDGE
    #else
    freopen("in.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    #endif

    cin>>s;
    int n=s.size();
    string ans;

    for(int i=0;i<(1<<n);i++){
        memset(vis,0,sizeof(vis));
        for(int j=0;j<n;j++){
            if(i&(1<<j))
                vis[j]=1;
        }
        string temp;
        temp.clear();
        for(int j=0;j<n;j++){
            if(vis[j])  continue;
            temp+=s[j];
        }
        if(temp[0]=='0')    continue;
        int t=getnum(temp);
        if(!t)  continue;
        //cout<<temp<<" "<<t<<endl;
        if((ll)sqrt(t)*(ll)sqrt(t)==t){
            //cout<<sqrt(t)<<endl;
            if(ans.size()==0)   ans=temp;
            else if(temp.size()>ans.size())
                ans=temp;
        }
    }
    //cout<<ans<<endl;
    if(ans.size())
        cout<<s.size()-ans.size()<<endl;
    else
        cout<<-1<<endl;


    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值