牛客網——華為機試(題5:進位制轉換)(Java)
阿新 • • 發佈:2018-12-19
題目描述:
寫出一個程式,接受一個十六進位制的數值字串,輸出該數值的十進位制字串。(多組同時輸入 )
輸入描述:
輸入一個十六進位制的數值字串。
輸出描述:
輸出該數值的十進位制字串。
示例1:
輸入:
0xA
輸出:
10
import java.util.Scanner; public class Main { public static void main (String[] args) { Scanner in = new Scanner(System.in); while(in.hasNext()) { String s = in.next(); int sum = 0; for(int k = 2;k<s.length();k++) { if((int)(s.charAt(k))>=48&&(int)(s.charAt(k))<=57) { sum+=((int)s.charAt(k)-48)*Math.pow(16, (double)(s.length()-k-1)); } else { int n = (int)s.charAt(k)-55; sum+=n*Math.pow(16, (double)(s.length()-k-1)); } } System.out.println(sum); } in.close(); } }