3 条题解

  • 1
    @ 2024-1-7 13:46:01

    暴力枚举

    import java.util.Random;
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                int num = sc.nextInt();
                int ans;
                String str = Integer.toString(num);
                int x = (int)(Math.sqrt(num));
                if(num == 1) System.out.println(1);
                else {
                    if(x * x == num){
                        System.out.println(0);
                    }else {
                        boolean flag = true;
                        while (x > 0){
                            int l = 0;//双指针
                            int r = 0;
                            flag = true;
                            String a1 = Integer.toString(--x * x);
                            while(r < a1.length()){
                                if(l >= str.length()){
                                    flag = false;
                                    break;
                                }
                                if(str.charAt(l) == a1.charAt(r)){
                                    l++;
                                    r++;
                                }else {
                                    l++;
                                }
                            }
                            if(flag) {
                                break;
                            }
                        }
                        String s = Integer.toString(x * x);
                        ans = str.length() - s.length();
                        if(flag){
                            if(x == 0) System.out.println(-1);
                            else System.out.println(ans);
                        }
                        else System.out.println(-1);
                    }
            }
        }
    }
    
    • 0
      @ 2024-1-7 14:33:48

      枚举从1到n的平方数

      #include<bits/stdc++.h>
      using namespace std;
      #define ll long long
      #define endl '\n'
      void solve();
      
      int main(){
      	ios::sync_with_stdio(0);
      	cin.tie(0);
      	cout.tie(0);
      	int t=1;
      //	cin>>t;
      	while(t--){
      		solve();
      	}
      	return 0;
      }
      
      void solve(){
      	int n;
      	cin>>n;
      	string s=to_string(n);
      	int ans=-1;
      	for(int i=1;i<=n/i;i++){
      		int t=i*i;
      		stack<char>st;
      		while(t){
      			st.push(t%10+'0');
      			t/=10;
      		}
      		int l=st.size();
      		for(auto c:s){
      			if(st.size()&&c==st.top())
      				st.pop();
      		}
      		if(st.empty()){
      			ans=max(ans,l);
      		}
      	}
      	if(ans==-1)
      		cout<<ans<<endl;
      	else
      		cout<<s.size()-ans<<endl;
      }
      
      • 0
        @ 2024-1-7 13:37:54

        二进制枚举,注意开头不能为零

        #include <iostream>
        #include <vector>
        #include <algorithm>
        #include <cmath>
        #include <map>
        #include <set>
        #include<stack>
        #include<queue>
        using namespace std;
        #define int long long
        const int MOD= 998244353;
        void solve() {
        	string s;cin>>s;
            int n=s.length();
            int mx=-1;
            for(int i=(1<<n)-1;i>0;i--){
                int num=0,ans=0;
               for(int j=0;j<n;j++){
                if((i>>(j))&1){
                    if(ans==0&&s[j]=='0'){
                        break;
                    }
                    ans++;
                    num=num*10+s[j]-'0';
                }
               }
            //    1881388645
               int x=sqrt(num);
               if(x*x==num&&num>0){
                mx=max(mx,ans);
               }
            }
            if(mx==-1){
            cout<<-1<<endl;
            }else{
                cout<<n-mx<<endl;
            }
        	return;
        }
        signed main()
        {
            iostream::sync_with_stdio(false);
            cin.tie(0);
            cout.tie(0);
            solve();
            // int t;cin>>t;
            // while(t--){
            //     solve();
            // }
            return 0;
        }
        
        • 1

        信息

        ID
        595
        时间
        1000ms
        内存
        256MiB
        难度
        7
        标签
        (无)
        递交数
        46
        已通过
        10
        上传者