写出一个程序,接受一个十六进制的数值字符串,输出该数值的十进制字符串(注意可能存在的一个测试用例里的多组数据)。
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNext()) { // 注意 while 处理多个 case String str = in.next(); String table = "0123456789ABCDEF"; str = str.substring(2); StringBuilder s = new StringBuilder(str); s.reverse(); int res = 0; char[] chs = s.toString().toCharArray(); for(int i = 0; i < chs.length; i++) { res += table.indexOf(chs[i]) * Math.pow(16, i); } System.out.println(res); } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s; while ((s= br.readLine())!=null){ System.out.println(Integer.parseInt(s.substring(2),16)); } } }
import java.util.*; import java.math.BigInteger; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ String s = sc.nextLine(); BigInteger b = new BigInteger(s.substring(2),16); System.out.println(b.toString(10)); } sc.close(); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String str1 = sc.nextLine(); //去掉0x标记 StringBuffer sb = new StringBuffer(str1); String str = sb.substring(2); //借用数组arr进行数字存储,如果为字母存为对应数字 char[] ch = str.toCharArray(); int length = ch.length; int[] arr = new int[length]; int count = -1; Integer ii = 0; for (char c : ch) { count++; if (c == 'A') { arr[count] = 10; } else if (c == 'B') { arr[count] = 11; } else if (c == 'C') { arr[count] = 12; } else if (c == 'D') { arr[count] = 13; } else if (c == 'E') { arr[count] = 14; } else if (c == 'F') { arr[count] = 15; } else { ii = Integer.parseInt(String.valueOf(c)); arr[count] = ii.intValue(); } } int zhi = arr.length - 1;//指数值 int sum = 0;//统计总和 for (int i = 0; i < arr.length; i++) { int fang = (int) Math.pow(16, zhi); zhi--; Integer result = Integer.valueOf(fang) * Integer.valueOf(arr[i]); int result1 = result.intValue(); sum += result1; } System.out.println(sum); } } }