You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							81 lines
						
					
					
						
							2.5 KiB
						
					
					
				
			
		
		
	
	
							81 lines
						
					
					
						
							2.5 KiB
						
					
					
				| import java.util.Scanner;
 | |
| 
 | |
| public class Main {
 | |
| 
 | |
|     // Helper function to convert a number from any base to decimal
 | |
|     public static long convertToDecimal(String number, int base) {
 | |
|         long result = 0;
 | |
|         int power = 0;
 | |
| 
 | |
|         // Traverse the number from right to left
 | |
|         for (int i = number.length() - 1; i >= 0; i--) {
 | |
|             char digit = number.charAt(i);
 | |
|             int value = 0;
 | |
| 
 | |
|             // Convert character to integer value
 | |
|             if (Character.isDigit(digit)) {
 | |
|                 value = digit - '0';
 | |
|             } else {
 | |
|                 value = Character.toUpperCase(digit) - 'A' + 10;
 | |
|             }
 | |
| 
 | |
|             // Check if the value is within the allowed range for the given base
 | |
|             if (value >= base) {
 | |
|                 throw new IllegalArgumentException("Invalid digit for the given base");
 | |
|             }
 | |
| 
 | |
|             // Add the digit's value to the result
 | |
|             result += value * Math.pow(base, power);
 | |
|             power++;
 | |
|         }
 | |
| 
 | |
|         return result;
 | |
|     }
 | |
| 
 | |
|     // Helper function to convert a number from decimal to any base
 | |
|     public static String convertFromDecimal(long number, int base) {
 | |
|         if (number == 0) {
 | |
|             return "0";
 | |
|         }
 | |
| 
 | |
|         StringBuilder result = new StringBuilder();
 | |
|         char[] digits = "0123456789ABCDEF".toCharArray();
 | |
| 
 | |
|         while (number > 0) {
 | |
|             int remainder = (int) (number % base);
 | |
|             result.insert(0, digits[remainder]);
 | |
|             number /= base;
 | |
|         }
 | |
| 
 | |
|         return result.toString();
 | |
|     }
 | |
| 
 | |
|     public static void main(String[] args) {
 | |
|         Scanner scanner = new Scanner(System.in);
 | |
| 
 | |
|         // Input the number X in any base Rx
 | |
|         System.out.print("输入数字X: ");
 | |
|         String numberX = scanner.nextLine();
 | |
|         System.out.print("Enter the base Rx (2-16): ");
 | |
|         int baseX = scanner.nextInt();
 | |
| 
 | |
|         // Input the target base Ry
 | |
|         System.out.print("Enter the target base Ry (2-16): ");
 | |
|         int baseY = scanner.nextInt();
 | |
| 
 | |
|         try {
 | |
|             // Convert X to decimal
 | |
|             long decimalValue = convertToDecimal(numberX, baseX);
 | |
| 
 | |
|             // Convert decimal to target base Y
 | |
|             String numberY = convertFromDecimal(decimalValue, baseY);
 | |
| 
 | |
|             // Output the result
 | |
|             System.out.println("The number " + numberX + " in base " + baseX + " is " + numberY + " in base " + baseY);
 | |
|         } catch (IllegalArgumentException e) {
 | |
|             System.out.println(e.getMessage());
 | |
|         }
 | |
| 
 | |
|         scanner.close();
 | |
|     }
 | |
| } |