https://pintia.cn/problem-sets/994805260223102976/problems/994805302786899968
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int num = scanner.nextInt(); int N1 = positiveOrder(num); int N2 = reverseOrder(num); int result=0; while ((result=(N2 - N1))!=6174){ if (result==0) break; System.out.printf("%04d - %04d = %04d\n",N2,N1,result); N1 = positiveOrder(result); N2 = reverseOrder(result); } //输出等于0或者6174的情况 System.out.printf("%04d - %04d = %04d",N2,N1,result); } //递增排序 public static int positiveOrder ( int num){ int[] arr = new int[4]; for (int i = 0; i < 4; i++) { arr[i] = num % 10; num = num / 10; } Arrays.sort(arr); return arr[0] * 1000 + arr[1] * 100 + arr[2] * 10 + arr[3]; } //非递增排序 public static int reverseOrder ( int num){ int[] arr = new int[4]; for (int i = 0; i < 4; i++) { arr[i] = num % 10; num = num / 10; } Arrays.sort(arr); return arr[3] * 1000 + arr[2] * 100 + arr[1] * 10 + arr[0]; } }