题解 | #进制转换#
进制转换
http://www.nowcoder.com/practice/8f3df50d2b9043208c5eed283d1d4da6
import java.util.Scanner;
public class Main{
public static void main(String []args){
Scanner in = new Scanner(System.in);
String str = in.nextLine();
int num = 0;
int factorial = 1;
if(str.startsWith("0x") == true || str.startsWith("0X") == true){
for(int i = str.length() - 1; i > 1; i--){
int temp = (int)str.charAt(i);
if(97 <= temp && temp <= 102){
num += (temp - 87) * factorial;
}
else if(65 <= temp && temp <= 70){
num += (temp - 55) * factorial;
}
else if(48 <= temp && temp <= 57){
num += (temp - 48) * factorial;
}
else{
System.out.println("Error");
}
factorial *= 16;
}
System.out.print(num);
}
in.close();
}
}
public class Main{
public static void main(String []args){
Scanner in = new Scanner(System.in);
String str = in.nextLine();
int num = 0;
int factorial = 1;
if(str.startsWith("0x") == true || str.startsWith("0X") == true){
for(int i = str.length() - 1; i > 1; i--){
int temp = (int)str.charAt(i);
if(97 <= temp && temp <= 102){
num += (temp - 87) * factorial;
}
else if(65 <= temp && temp <= 70){
num += (temp - 55) * factorial;
}
else if(48 <= temp && temp <= 57){
num += (temp - 48) * factorial;
}
else{
System.out.println("Error");
}
factorial *= 16;
}
System.out.print(num);
}
in.close();
}
}