zoukankan      html  css  js  c++  java
  • codeforces-962-c

    题意:给你一个数,问从中删除某几位数字后重新组成的数字是否是某个数的平方;

    解题思路:数据小,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
    ";
        else
        {
    
        while(z)
        {
            cnt++;
            a[cnt]=z%10;
            z=z/10;
        }
        dfs(0,0,0);
        if(ans==999999)
            cout<<"-1
    ";
        else
            cout<<ans<<endl;
        }
    }
    

      

  • 相关阅读:
    OMNETPP: tictoc
    OMNETPP安装
    Unified SR
    SCM
    DC tunnel
    AIMD
    AQM
    MANAGER POJ1281 C语言
    Pascal Library C语言 UVALive3470
    The 3n + 1 problem C语言 UVA100
  • 原文地址:https://www.cnblogs.com/huangdao/p/8799025.html
Copyright © 2011-2022 走看看