题解 | #进制转换# 先截取字符串,再分别判断
进制转换
http://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.hasNextLine()){ String s=sc.nextLine(); String s1=s.substring(2,s.length()); int ans=0; for(int i=0;i<s1.length();i++){ double power=Math.pow(16,(s1.length()-i-1)); if(s1.charAt(i)>='A'&&s1.charAt(i)<='F'){ ans+=(s1.charAt(i)-'A'+10)*power; }else if(s1.charAt(i)>='a'&&s1.charAt(i)<='f'){ ans+=(s1.charAt(i)-'a'+10)*power; }else{ String a=s1.charAt(i)+""; ans+=Integer.parseInt(a)*power; } } System.out.println(ans); } } }