#include<stdio.h>#include<algorithm>Usingnamespace Std;Constint aim=6174;int n;int arr[4];BOOL Nonincreasingorder (int A,int b) {return a>b;}BOOL Nondecreasingorder (int A,int b) {return a<b;}int Tonum ()Get this number {int sum=0;Forint i=0; i<4; ++i) {sum=sum*10+arr[i]; }return sum;}void Toarr () {Forint i=0; i<4; ++i)Disassemble number {arr[i]=n%10; N/=10; }}int main () {scanf ("%d", &n);int Min,max;//the minimum number of permutations, the maximum number of permutations while (1) {Toarr (); //disassembly sort (arr,arr+4,nonincreasingorder); //generate the maximum number max=tonum (); Sort (arr,arr+4,nondecreasingorder); //generates a minimum number of min=tonum (); N=max-min; printf ( "%04d-%04d =%04d\n if (n== 0 | | N==aim) //n is 4 identical numbers (including 0000 without extra consideration), or cut it to the end. break;} return 0;}
pat:1069. The Black Hole of Numbers (+) AC