题解:
1. 将输入的数字拆分装进数组
2. 判断数组上的各位数字是否相等
3. 是 输出 n - n = 0000
4..否 将num循环重复 1 ;将数组排序,得到降序A,升序B;进而得到num; 直至 num == 6174
import java.io.*;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws IOException {
// 使用io流包装
StreamTokenizer in =new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
in.nextToken();
int N=(int)in.nval,n=N;
int[] arr=new int[4];
int flag=1;
for (int i = 0; i < 4; i++) {
arr[i]=N%10;
N/=10;
if(i>0&&arr[i-1]!=arr[i])flag=0;
}
if (flag==1) System.out.printf("%04d - %04d = 0000\n",n,n);
else {
int A,B,num=n;
while (true){
for (int i = 0; i < 4; i++) {
arr[i]=num%10;
num/=10;
}
Arrays.sort(arr);
A=0;B=0;
for (int i = 0; i <4 ; i++) {
A=A*10+arr[3-i];
B=B*10+arr[i];
}
num=A-B;
System.out.printf("%04d - %04d = %04d\n",A,B,num);
if (num==6174)break;
}
}
}
}