题解 | #进制转换#
进制转换
http://www.nowcoder.com/practice/8f3df50d2b9043208c5eed283d1d4da6
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { String str = in.next(); char[] arr = str.toCharArray(); if (arr[0] == '0' && arr[1] == 'x') { int x=进制转换(arr); System.out.println(x); } } } public static int 进制转换(char[] ary) { int COUNT = 0; int k=0; for (int i = ary.length - 1; i >= 2; i--) { COUNT = (int)(当前字符数值(ary[i]) *Math.pow(16,k )+ COUNT); k++; } return COUNT; } public static int 当前字符数值(char sh) { int cnt=0; switch (sh) { case '0':cnt= 0;break; case '1':cnt= 1;break; case '2':cnt= 2;break; case '3':cnt= 3;break; case '4':cnt= 4;break; case '5':cnt= 5;break; case '6':cnt= 6;break; case '7':cnt= 7;break; case '8':cnt= 8;break; case '9':cnt= 9;break; case 'A':cnt= 10;break; case 'B':cnt= 11;break; case 'C':cnt= 12;break; case 'D':cnt= 13;break; case 'E':cnt= 14;break; case 'F':cnt= 15;break; } return cnt; } }