zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 42 (Rated for Div. 2) C

    C. Make a Square
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You are given a positive integer nn, written without leading zeroes (for example, the number 04 is incorrect).

    In one operation you can delete any digit of the given integer so that the result remains a positive integer without leading zeros.

    Determine the minimum number of operations that you need to consistently apply to the given integer nn to make from it the square of some positive integer or report that it is impossible.

    An integer xx is the square of some positive integer if and only if x=y2x=y2 for some positive integer yy.

    Input

    The first line contains a single integer nn (1n21091≤n≤2⋅109). The number is given without leading zeroes.

    Output

    If it is impossible to make the square of some positive integer from nn, print -1. In the other case, print the minimal number of operations required to do it.

    Examples
    input
    Copy
    8314
    output
    Copy
    2
    input
    Copy
    625
    output
    Copy
    0
    input
    Copy
    333
    output
    Copy
    -1
    Note

    In the first example we should delete from 83148314 the digits 33 and 44. After that 83148314 become equals to 8181, which is the square of the integer 99.

    In the second example the given 625625 is the square of the integer 2525, so you should not delete anything.

    In the third example it is impossible to make the square from 333333, so the answer is -1.

    题意:判断x是否是一个完全平方数,如果不是,最少能删去几个数让他是完全平方数,如果有,输出删去的最少操作次数,若没有,输出-1;

    思路:暴力枚举,从1开始,到sqrt(x)位置,看是否满足这个数是x的子串,如果是,存下它的操作次数与min进行比较,输出 min;

    小知识点:to_string 函数,可以将一串数字转化成字符串

    #include<iostream>  
    #include<string>  
      
    using namespace std;  
      
    int main()  
    {  
        string s1 = "2018.11";  
        int a1 = stoi(s1);  
        double c = stod(s1);  
        float d = stof(s1);  
        int a2 = a1 + 1;  
        string b = to_string(a1);  
        b.append(" is a string");  
          
        cout << a1 <<"/"<<c<<"/"<<d<<"/"<<a2<<"/"<<b<< endl;  
    }  

    AC代码

    #include <bits/stdc++.h>
    
    #define x first
    #define y second
    #define pb push_back
    #define mp make_pair
    #define low_b lower_bound
    #define up_b upper_bound
    #define all(v) v.begin(), v.end()
    
    using namespace std;
    
    typedef long long ll;
    typedef long double ld;
    typedef pair<int,int> pii;
    typedef pair<ll,int> pli;
    typedef pair<ll,ll> pll;
    
    inline void Kazakhstan(){
        ios_base :: sync_with_stdio(0);
        cin.tie(0), cout.tie(0);
    }
    
    const int N = 2e9;
    
    int main(){
        Kazakhstan();
        string s;
    
        cin >> s;
        int mn = INT_MAX;
        for(int i = 1; i * i <= N; i++){
            string t = to_string(i * i);
            int k = 0;
            bool w = 0;
            for(int j = 0; j < s.size(); j++){
                if(t[k] == s[j])k++;
                if(k == t.size()){ 
                    w = 1;
                    break;
                }
            }
            if(w){
                mn = min(mn, int(s.size() - t.size())); 
            }
        }
        if(mn == INT_MAX)cout << "-1";
        else cout << mn;
    }
    View Code
    每一个不曾刷题的日子 都是对生命的辜负 从弱小到强大,需要一段时间的沉淀,就是现在了 ~buerdepepeqi
  • 相关阅读:
    解决Cell重绘导致 重复的问题
    给Cell间隔颜色
    NSUserDefault 保存自定义对象
    xcode6 下载
    unrecognized selector sent to instance
    16进制颜色转换
    local unversioned, incoming add upon update问题
    应用崩溃邮件通知
    TabBar变透明
    代码手写UI,xib和StoryBoard间的博弈,以及Interface Builder的一些小技巧
  • 原文地址:https://www.cnblogs.com/buerdepepeqi/p/8795357.html
Copyright © 2011-2022 走看看