当前位置: 代码迷 >> 综合 >> HDOJ 1335 Basically Speaking
  详细解决方案

HDOJ 1335 Basically Speaking

热度:76   发布时间:2023-10-21 19:31:45.0

HDACM1335
水…

import java.util.Scanner;public class Main{static char c[] = {
   '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};public static void main(String[] args) {Scanner sc = new Scanner(System.in);while (sc.hasNext()) {String num = sc.next();int n = sc.nextInt();int m = sc.nextInt();System.out.printf("%7s",hexConversion(num, n, m));System.out.println();}sc.close();}public static String hexConversion(String num , int n , int m){char ch[] = num.toCharArray();int sum = 0;for (int i = ch.length-1; i >=0 ; i--) {for (int j = 0; j < n; j++) {if (ch[i]==c[j]) {sum += j*(int)Math.pow(n, ch.length-i-1);}}}num = "";while (sum>0) {num = c[sum%m]+num;sum /= m ;}if (num.length()<8) {return num;}return "ERROR";}
}