Java题解—1019 数字黑洞 (20 分)
作者:互联网
原题
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];
}
}
标签:10,arr,20,int,题解,04d,num,1019,result 来源: https://www.cnblogs.com/InifiteVictory/p/15528543.html