题意:给你一个数,问从中删除某几位数字后重新组成的数字是否是某个数的平方;
解题思路:数据小,dfs直接搜,每位数只有两种选择,要或者不要
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#define ll long long
using namespace std;
ll a[20];
int ans=999999;
int cnt;
void dfs(int step,ll x,int len)
{
if(step>cnt)
return;
int m=sqrt(x);
int tx=x;
if(m*m==x&&x!=0)
{
int t=0;
while(tx)
{
t++;
tx=tx/10;
}
ans=min(ans,cnt-t);
}
dfs(step+1,x,len);
dfs(step+1,x+a[step+1]*pow(10,len),len+1);
}
int main()
{
ll n;
ll z;
cnt=0;
cin>>n;
z=n;
int q=sqrt(n);
if(q*q==n)
cout<<"0\n";
else
{
while(z)
{
cnt++;
a[cnt]=z%10;
z=z/10;
}
dfs(0,0,0);
if(ans==999999)
cout<<"-1\n";
else
cout<<ans<<endl;
}
}