Educational Codeforces Round 42 (Rated for Div. 2)-C. Make a Square

本文介绍了一种算法,用于确定通过删除特定数字将给定正整数转换为平方数所需的最小操作次数。若无法转换,则输出-1。算法通过遍历接近目标数的平方数并逐位比较来实现。
C. Make a Square
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input

output
standard output
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.

题意:找出一个数最少删除多少个数字后可以变成一个平方数。如果不能成为平方数,则输出 -1。
题解:首先从最接近这个数的平方数开始遍历,判断方法是一个位数一个位数判断,如果相同则都判断下一位数,否则只有n判断下一个位数。不过要注意如果结束后n前面还剩位数,要把前面的位数加上去。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;

int main()
{
    int n;
    int x,y,num;
    int flag;
    int cont;
    int k;
    while(scanf("%d",&k) != EOF)
    {
        flag = 0;
        int ii = sqrt(k);
        for(int i = ii;i >= 1 && !flag;i--)
        {
            n = k;
            cont = 0;
            num = i * i;
            while(1)
            {
                if(num <= 0)
                {
                    while(n > 0)
                    {
                        n /= 10;
                        cont++;
                    }
                    flag = 1;
                    break;
                }
                if(n <= 0)
                {
                    break;
                }
                x = n % 10;
                y = num % 10;
                if(x == y)
                {
                    n /= 10;
                    num /= 10;
                }
                else
                {
                    cont++;
                    n /= 10;
                }
            }
        }
        if(flag)
        {
            printf("%d\n",cont);
        }
        else
        {
            printf("-1\n");
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值