#include <stdio.h>
#include <string.h>
#include <math.h>
int a[4];//不需用字符数组
int M;
int m1,m2;
int fun1(int num){
int i,j;
for(i=0;i<4;i++){
a[i]=num%10;
num=num/10;
}
int max,temp;
for(i=0;i<3;i++){
max=i;
for(j=i+1;j<4;j++)
if(a[j]>a[max])
max=j;
temp=a[max];
a[max]=a[j];
a[j]=temp;
}//看过其他人的代码,这里排序用的是指针,能AC。可是我不用指针就会超时,是因为这个原因吗?还是因为其他的步骤不对?
m1=a[0]*1000+a[1]*100+a[2]*10+a[3];
m2=a[3]*1000+a[2]*100+a[1]*10+a[0];
M=m1-m2;
return M;
}
int main(){
scanf("%d",&M);//升序和降序相反,只需一函数
//由循环得出数组排序,只用输出结果为数字
if(M==6174){
printf("7641 - 1467 = 6174") ;
}
while(M!=6174){
M=fun1(M);
if(M==0)
{printf("%d-%d=0000",m1,m2);
break;
}
else
{printf("%d%d%d%d-%d%d%d%d=%d%d%d%d",a[0],a[1],a[2],a[3],a[3],a[2],a[1],a[0],M/1000,M/100%10,M/10%10,M%10);
}
}
return 0;
}
贴出你的代码,需要调试下看看是不是程序中有死循环。