【哈尔滨学院ACM-20211126训练赛】个人题解

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值