import java.math.BigInteger; import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while(scanner.hasNext()) { //将m进制的数转化成n进制的数 int m = scanner.nextInt(); int n = scanner.nextInt(); String x = scanner.next(); Map<Character, Integer> myMap = new HashMap<Character, Integer>(); myMap.put('0',0);myMap.put('1',1 );myMap.put('2',2 );myMap.put('3',3 );myMap.put('4', 4); myMap.put('5',5 );myMap.put('6', 6);myMap.put('7',7 );myMap.put('8',8 );myMap.put('9',9 ); myMap.put('A',10 );myMap.put('B',11 );myMap.put('C', 12);myMap.put('D',13 );myMap.put('E',14 ); myMap.put('F',15 );myMap.put('G',16 );myMap.put('H',17 );myMap.put('I',18 );myMap.put('J',19 ); myMap.put('K',20 );myMap.put('L',21 );myMap.put('M',22 );myMap.put('N',23 );myMap.put('O',24 ); myMap.put('P',25 );myMap.put('Q',26 );myMap.put('R',27 );myMap.put('S',28 );myMap.put('T',29 ); myMap.put('U',30 );myMap.put('V',31 );myMap.put('W',32 );myMap.put('X',33 );myMap.put('Y',34 ); myMap.put('Z', 35); BigInteger bi = BigInteger.ZERO; for (int i = x.length()-1; i >= 0; i--) { bi = bi.add(BigInteger.valueOf((long)(myMap.get(x.charAt(i)) * Math.pow(m, x.length()-i-1) )) ); } System.out.println(bi.toString(n)); } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int m = in.nextInt(); int n = in.nextInt(); String x = in.next(); String table = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //1、先将M进制的数转换为10进制的数 : num StringBuilder str = new StringBuilder(x); str.reverse(); long num = 0; char[] chs = str.toString().toCharArray(); for(int i = 0; i < chs.length; i++) { num += table.indexOf(chs[i]) * Math.pow(m, i); } // System.out.println(num); if(num == 0) { System.out.println("0"); } else { //将num转换成n进制的数 str = new StringBuilder(); while(num != 0) { long t = num % n; if(t > 9) { str.append((char)(table.charAt((int)t) + 32)); } else { str.append(table.charAt((int)t)); } num /= n; } str.reverse(); System.out.println(str.toString()); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()){ int m = scanner.nextInt(); int n = scanner.nextInt(); String num = scanner.next(); long i = Long.parseLong(num, m); System.out.println(Long.toString(i,n)); } } }