蓝桥杯- 基础练习:十六进制转十进制

 

import java.util.Scanner;

public class Main {

    
    public static void main(String[] args) {
        String string = new Scanner(System.in).next();
        String[] strings = new String[string.length()];
        for (int i = 0; i < string.length(); i++) {
            strings[i] = String.valueOf(string.charAt(i)).toUpperCase();
        }
        for (int i = 0; i < strings.length; i++) {
            if (strings[i].equals("A")) {
                strings[i] = "10";
            }
            if (strings[i].equals("B")) {
                strings[i] = "11";
            }
            if (strings[i].equals("C")) {
                strings[i] = "12";
            }
            if (strings[i].equals("D")) {
                strings[i] = "13";
            }
            if (strings[i].equals("E")) {
                strings[i] = "14";
            }
            if (strings[i].equals("F")) {
                strings[i] = "15";
            }

        }
        long num = 0;
        for (int j = 0; j < strings.length; j++) {
            num += Integer.parseInt(strings[j])*f(strings.length-1-j);
        }
        System.out.println(num);
    }

    private static long f(long i) {
        if (i==1) {
            return 16;
        }
        if (i==0) {
            return 1;
        }
        
        return     16*f(i-1);
    }
    
}

 

posted @ 2018-01-13 21:26  Bap  阅读(331)  评论(0编辑  收藏  举报