第一次打cf:造偶数

本文介绍了如何通过反转数字前缀来使一个不包含0的整数变成偶数,分析了不同情况下的最少操作次数。提供了一个C++代码示例,展示了解决这个问题的逻辑,包括当数字已经是偶数、最高位为偶数、中间存在偶数以及所有尝试都无法变为偶数的情况。
摘要由CSDN通过智能技术生成

A. Make Even

Polycarp has an integer n that doesn't contain the digit 0. He can do the following operation with his number several (possibly zero) times:

Reverse the prefix of length l (in other words, l leftmost digits) of n. So, the leftmost digit is swapped with the l-th digit from the left, the second digit from the left swapped with (l−1)-th left, etc. For example, if n=123456789 and l=5, then the new value of n will be 543216789.

Note that for different operations, the values of l can be different. The number l can be equal to the length of the number n — in this case, the whole number n is reversed.

Polycarp loves even numbers. Therefore, he wants to make his number even. At the same time, Polycarp is very impatient. He wants to do as few operations as possible.

Help Polycarp. Determine the minimum number of operations he needs to perform with the number n to make it even or determine that this is impossible.

You need to answer t independent test cases.

input:

The first line contains the number t (1≤t≤104) — the number of test cases.

Each of the following t lines contains one integer n (1≤n<109). It is guaranteed that the given number doesn't contain the digit 0.

Output

Print t lines. On each line print one integer — the answer to the corresponding test case. If it is impossible to make an even number, print -1.

example:

Input

4

3876

387

4489

3

output

0

2

1

-1

Note:

In the first test case, n=3876, which is already an even number. Polycarp doesn't need to do anything, so the answer is 0.

In the second test case, n=387. Polycarp needs to do 2 operations:

1.Select l=2 and reverse the prefix 38–––7. The number n becomes 837. This number is odd.

2.Select l=3 and reverse the prefix 837––––. The number n becomes 738. This number is even.

It can be shown that 2 is the minimum possible number of operations that Polycarp needs to do with his number to make it even.

In the third test case, n=4489. Polycarp can reverse the whole number (choose a prefix of length l=4). It will become 9844 and this is an even number.

In the fourth test case, n=3. No matter how hard Polycarp tried, he would not be able to make an even number.

思路:

任何情况上最多反转两次即可:

1.本身是偶数(最低位是偶数)直接输出"0";

2.最高位是偶数,直接完全翻转;操作1次,输出"1";

3.中间有偶数,和开头(最高位)翻转一波,然后就变成了第2种情况,总共操作2次,输出"2";

代码:

#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>

using namespace std;
const int N = 10e7;
char a[N];

int main()
{
    int T;
    int len;
    int i;
    int cnt;
    int flag;
    cin >> T;
    while(T --)
    {
        flag = 0;
        cin >> a;
        len = strlen(a);
        for(i = 0;i < len;i ++)
        {
            if(a[i] % 2== 0)       //发现偶数就跳,偶数在哪个位置是这个题的核心,直接决定了输出几,跳奇数没啥用 ;
            {
                flag = 1;
            }
        }
        if(a[len -1] % 2 == 0)   //发现最低位是偶数,直接输出0;
        {
            cout << "0" << endl;
        }
        else if(a[0] % 2 == 0)  //最低位不是偶数,但最高位是偶数,输出1;
        {
            cout << "1" << endl;
        }
        else if(flag == 0)      //最低位和最高位不是偶数,flag == 0(中间也不是偶数),输出-1
        {
             cout << "-1" << endl;
        }
        else
        {
            cout << "2" << endl;  //这个题已经分析出来最大就是2,其他都情况都解决了,放心大胆else (正难则反)
        }  
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

21RGHLY

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值