第一次打cf:造偶数

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

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;
}

题目描述 图像是由很多的像素点组成的。如果用 0 表示黑,255 表示白,0 和 255 之间的值代表不同程度的灰色,则可以用一个字节表达一个像素(取值范围为十进制 0-255、十六进制 00-FF)。这样的像素组成的图像,称为 256 级灰阶的灰度图像。 现在希望将 256 级灰阶的灰度图像压缩为 16 级灰阶,即每个像素的取值范围为十进制 0-15、十六进制 0-F。压缩规则为:统计出每种灰阶的数量,取数量最多的前 16 种灰阶(如某种灰阶的数量与另外一种灰阶的数量相同,则以灰阶值从小到大为序),分别编号 0-F(最多的编号为 0,以此类推)。其他灰阶转换到最近的 16 种灰阶之一,将某个点的灰阶值(灰度,而非次数)与 16 种灰阶中的一种相减,绝对值最小即为最近,如果绝对值相等,则编号较小的灰阶更近。 输入格式 输入第 1 行为一个正整数 n(10≤n≤20),表示接下来有 n 行数据组成一副 256 级灰阶的灰度图像。 第 2 行开始的 n 行,每行为长度相等且为偶数的字符串,每两个字符用十六进制表示一个像素。约定输入的灰度图像至少有 16 种灰阶。约定每行最多 20 个像素。 输出格式 第一行输出压缩选定的 16 种灰阶的十六进制编码,共计 32 个字符。 第二行开始的 n 行,输出压缩后的图像,每个像素一位十六进制数表示压缩后的灰阶值。 输入输出样例 输入 #1复制 10 00FFCFAB00FFAC09071B5CCFAB76 00AFCBAB11FFAB09981D34CFAF56 01BFCEAB00FFAC0907F25FCFBA65 10FBCBAB11FFAB09981DF4CFCA67 00FFCBFB00FFAC0907A25CCFFC76 00FFCBAB1CFFCB09FC1AC4CFCF67 01FCCBAB00FFAC0F071A54CFBA65 10EFCBAB11FFAB09981B34CFCF67 01FFCBAB00FFAC0F071054CFAC76 1000CBAB11FFAB0A981B84CFCF66 输出 #1复制 ABCFFF00CB09AC07101198011B6776FC 321032657CD10E 36409205ACC16D B41032657FD16D 8F409205ACF14D 324F326570D1FE 3240C245FC411D BF4032687CD16D 8F409205ACC11D B240326878D16E 83409205ACE11D 说明/提示 【样例 1 解释】 灰阶 AB、CF 和 FF 出现 14 次,00 出现 10 次,CB 出现 9 次,09 出现 7 次,AC 出现 6 次,07 出现 5 次,10、11 和 98 出现 4 次,01、1B、67、76 和 FC 出现 3 次。
11-14
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

21RGHLY

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

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

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

打赏作者

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

抵扣说明:

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

余额充值