陷阱题。很无语。。。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,ti,a,b,s[5];
bool cmp(int a,int b){
return a>b;
}
int main(){
int pos;
while(scanf("%d",&n)&&n!=-1){
int k=1;
pos=1;
memset(s,0,sizeof(s));
for(int c=n;c;c/=10){
s[pos++]=c%10;
}
printf("N=%d:\n",n);
if(s[1]==s[2]&&s[2]==s[3]&&s[2]==s[4]||pos!=5){
printf("No!!\n");
continue;
}
ti=0;
while(n!=6174&&n){
sort(s+1,s+pos);
k=1;
a=0;
for(int i=pos-1;i>0;i--){
a+=s[i]*k;
k*=10;
}
sort(s+1,s+pos,cmp);
k=1;
b=0;
for(int i=pos-1;i>0;i--){
b+=s[i]*k;
k*=10;
}
n=b-a;
ti++;
pos=1;
printf("%d-%d=%d\n",b,a,n);
for(int c=n;c;c/=10){
s[pos++]=c%10;
}
}
printf("Ok!! %d times\n",ti);
}
return 0;
}