华为OD机试(yyyy-mm) 题目一

题目:

2-35进制的两个数进行相减,大于10进制的用a-z(小写字母)补齐

输入:
2   11    1
解释:

2 为2进制, 11 为被减数 1 为减数

输出:
0 10
解释:

①0为正数,1为负数 ②11-1 =10

public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int x = input.nextInt();
        String a = input.next();
        String b = input.next();

        int ax = Integer.parseInt(a,x);
        int bx = Integer.parseInt(b,x);

        if (ax - bx >= 0){
            System.out.println(0+" "+toX((ax-bx),x));
        }else {
            System.out.println(1+" "+toX((bx-ax),x));
        }
    }
    public static String toX(int a, int x){
        List<String> list = new ArrayList<>();
        if (x <= 10){
            while (a != 0){
                list.add(String.valueOf(a%x));
                a = a / x ;
            }
            int sum = 0;
            for (int i = 0; i < list.size(); i++){
                int v = (int) Math.pow(10, i);
                sum += Integer.parseInt(list.get(i))*v;
            }
            return String.valueOf(sum);
        }
        else {
            while (a != 0){
                if ((a%x)>=10){
                    list.add(intToA(a%x));
                }else {
                    list.add(String.valueOf(a%x));
                }
                a = a / x ;
            }
            String s = "";
            for (int i = list.size()-1; i >= 0; i--){
                s = s + list.get(i);
            }
            return s;
        }
    }

    public static String intToA(int a){
        if (a == 10){
            return "A";
        }
        if (a == 11){
            return "B";
        }
        if (a == 12){
            return "C";
        }
        if (a == 13){
            return "D";
        }
        if (a == 14){
            return "E";
        }
        if (a == 15){
            return "F";
        }
        if (a == 16){
            return "G";
        }
        if (a == 17){
            return "H";
        }
        if (a == 18){
            return "I";
        }
        if (a == 19){
            return "J";
        }
        if (a == 20){
            return "K";
        }
        if (a == 21){
            return "L";
        }
        if (a == 22){
            return "M";
        }
        if (a == 23){
            return "N";
        }
        if (a == 24){
            return "O";
        }
        if (a == 25){
            return "P";
        }
        if (a == 26){
            return "Q";
        }
        if (a == 27){
            return "R";
        }
        if (a == 28){
            return "S";
        }
        if (a == 29){
            return "T";
        }
        if (a == 30){
            return "U";
        }
        if (a == 31){
            return "V";
        }
        if (a == 32){
            return "W";
        }
        if (a == 33){
            return "X";
        }
        if (a == 34){
            return "Y";
        }
        if (a == 35){
            return "Z";
        }
        return "";
    }
上一篇:[ScyllaHide] 00 简单介绍和使用


下一篇:《逆向分析实战》——随笔笔记