聽書網(wǎng)頁設(shè)計(jì)教程成都seo
目錄
- 第一章、算法題
- 1.1)題目描述
- 1.2)解題思路與答案
- 1.3)派仔的解題思路與答案
- 1.3)牛客鏈接
友情提醒:
先看文章目錄,大致了解文章知識(shí)點(diǎn)結(jié)構(gòu),點(diǎn)擊文章目錄可直接跳轉(zhuǎn)到文章指定位置。
第一章、算法題
1.1)題目描述
題目描述:
寫出一個(gè)程序,接受一個(gè)十六進(jìn)制的數(shù),輸出該數(shù)值的十進(jìn)制表示。
輸入描述:
輸入一個(gè)十六進(jìn)制的數(shù)值字符串。
輸出描述:
輸出該數(shù)值的十進(jìn)制字符串。不同組的測(cè)試用例用\n隔開。
示例:
1.2)解題思路與答案
解題思路:
①使用Java自帶的Integer.parseInt(String,16) 方法
答案:
import java.io.*;
import java.util.*;public class Main{public static void main(String[] args) throws Exception{Scanner sc = new Scanner(System.in);while(sc.hasNextLine()){String s = sc.nextLine();System.out.println(Integer.parseInt(s.substring(2,s.length()),16));}}
}
1.3)派仔的解題思路與答案
①res = res * BASE + map.get(ch); 這個(gè)公式是精華原理如圖:
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;public class Putest {private final static int BASE = 16;private static Map<Character, Integer> map = new HashMap<Character, Integer>(){{put('0', 0);put('1', 1);put('2', 2);put('3', 3);put('4', 4);put('5', 5);put('6', 6);put('7', 7);put('8', 8);put('9', 9);put('A', 10);put('B', 11);put('C', 12);put('D', 13);put('E', 14);put('F', 15);put('a', 10);put('b', 11);put('c', 12);put('d', 13);put('e', 14);put('f', 15);}};public static int getDecimal(String number) {int res = 0;char [] arr =number.toCharArray();for( int i=0;i<arr .length;++i){//這個(gè)公式是精華,map.get()方法返回指定鍵所映射的值res = res * BASE + map.get(arr[i]);}/* 也可以這樣寫循環(huán)for (char ch : number.toCharArray()) {//這個(gè)公式是精華,map.get()方法返回指定鍵所映射的值res = res * BASE + map.get(ch);}*/return res;}public static void main(String[] args) {Scanner in = new Scanner(System.in);while (in.hasNext()) {String number = in.next();int res = getDecimal(number.substring(2));System.out.println(res);}}
}
1.3)??玩溄?/h3>
牛客網(wǎng)鏈接