This is my first article about solve contest problems. Thus there may be some error in this article, it's welcome correct that. There is no solution to all the problems of the contest this time in this article.May be you should learn solving some problem in other article if it is not included by this.
Notice: This article is wrote and edited by Flex,myself. All rights reserved. Please contact me if reproduced.
A - Make Even
Tips:
It's true easy problem. we can do it by simple way.
For the first condition, we must sure it will print '0' if the original number is even.
Just a simple example: when will it output '-1'? you might know the answer: it only print '-1' when there is no even digit in number input.
Well, rule out the condition likes two examples above, we can sure one thing: if there are even digits in number input and it is not the last digit, the answer must be '2' wherever it is and no matter how much it. because we can move the even digit to the beginning of number by one step, and then move it to the end by one step.
So we can coding after understand principle above.
Code:
import java.util.Scanner;
import java.util.Vector;
public class Main {
public static Scanner input;
public static Vector<Integer> ans;
public static void main(String []args) {
input = new Scanner(System.in);
int t = input.nextInt();
ans = new Vector<>();
for(int idx = 0;idx < t;idx += 1) {
int num = input.nextInt();
if((num & 1) == 0) {
ans.add(0);
} else {
boolean is_ok = false;
while(num >= 10) {
if(((num % 10) & 1) == 0) {
is_ok = true;
}
num /= 10;
}
if((num & 1) == 0) {
ans.add(1);
continue;
}
if(is_ok) {
ans.add(2);
} else {
ans.add(-1);
}
}
}
for(Integer s : ans)
System.out.println(s);
input.close();
}
}