Generation

generate functionWed, 25 Jan 2023

Calculate the difference between the ``maximum integer value'' and the ``minimum integer value'' that can be obtained by rearranging the numbers of each digit for a positive integer value of 4 digits or less to create the next integer value. For example, the maximum integer value obtained by rearranging the digits of ``5138'' is ``8531'', the minimum integer value is ``1358'', and the difference between them is ``7173''. The next integer value will be "7173". Similarly, in the case of ``21'', the thousands and hundreds digits are considered to be ``0'' (which is ``0021''), and the maximum integer value that can be obtained by rearranging the numbers of each digit is ``2100'', and the smallest The integer value is ``12'' and the difference between them is ``2088'', so the next integer value for ``21'' is ``2088''. For a positive integer value of 4 digits or less, if you repeat the operation to create the next integer value by calculating the difference between the "maximum integer value" and the "minimum integer value" that can be obtained by rearranging the numbers of each digit , always converges to some integer value. Enter a positive integer value n of 4 or fewer digits in the command line input. Create a program that repeats the above calculation and outputs the integer values until convergence to the standard output.

import java.util.Scanner; import java.util.Arrays; public class Main { static String sort(String str) { char[] array = str.toCharArray(); Arrays.sort(array); String ans = ""; for (int i = 0; i < array.length; i++) { ans += array[i]; } return ans; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); while (true) { String str = Integer.toString(n); String maxStr = sort(str); String minStr = ""; for (int i = maxStr.length() - 1; i >= 0; i--) { minStr += maxStr.charAt(i); } int max = Integer.parseInt(maxStr);

Questions about programming?Chat with your personal AI assistant