ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Codeforces Round #756 (Div. 3) A. Make Even

2021-11-26 19:58:50  阅读:510  来源: 互联网

标签:Even even 756 nn Polycarp Make number case test


题目链接:Problem - 1611A - Codeforces

Polycarp has an integer nn 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, ll leftmost digits) of nn. So, the leftmost digit is swapped with the ll-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 nn will be 543216789.

Note that for different operations, the values of ll can be different. The number ll can be equal to the length of the number nn — in this case, the whole number nn 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 nn to make it even or determine that this is impossible.

You need to answer tt independent test cases.

Input

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

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

Output

Print tt 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

Copy

4
3876
387
4489
3

output

Copy

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 387. The number nn becomes 837. This number is odd.
  2. Select l=3 and reverse the prefix 837. The number nn 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 98449844 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,第一个是偶数是1,否则就是2

#include<bits/stdc++.h>
using namespace std;


int main(){ 
	int t;
	cin >> t;
	while(t--){
		string s;
		cin >> s;
		bool flag = 0;
		for(int i = 0; i < s.length(); i++){
			if(s[i] == '2' || s[i] == '0' || s[i] == '4' || s[i] == '6' || s[i] == '8'){
				flag = 1;
			}
		}
		if(flag == 0){
			cout << -1 << endl;
			continue;
		}
		int len = s.length();
		if(s[len - 1] == '0' || s[len - 1] == '2' || s[len - 1] == '4' || s[len - 1] == '6' || s[len - 1] == '8'){
			cout << 0 << endl;
		}else if((s[0] - '0') % 2 == 0){
			cout << 1 << endl;
		}else{
			cout << 2 << endl;
		}
	} 
	
	return 0;
}

标签:Even,even,756,nn,Polycarp,Make,number,case,test
来源: https://blog.csdn.net/m0_55682843/article/details/121567419

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有