题解 | #进制转换#简单易懂
进制转换
https://www.nowcoder.com/practice/8f3df50d2b9043208c5eed283d1d4da6
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String numStr = sc.nextLine().substring(2).toLowerCase();
int num = 0;
int m = 0;
for (int i = numStr.length() - 1; i >= 0; i--) {
char c = numStr.charAt(i);
int n;
if (c >= 'a' && c <= 'f') {
n = 10 + (c - 'a');
}else {
n = Integer.parseInt(c + "");
}
num += n * Math.pow(16, m++);
}
System.out.println(num);
}
}
}
查看9道真题和解析

