codeforces-962-c
题意:给你一个数,问从中删除某几位数字后重新组成的数字是否是某个数的平方;
解题思路:数据小,dfs直接搜,每位数只有两种选择,要或者不要
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | #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; } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步