2018-10-23 01:20

# PAT1019 数字黑洞 提交时Time Limit Exceeded

//PAT1019V1
#include

int FeiDiZeng(int n){
int a[4];
a[0]=n%10; //ge
a[1]=n/10%10; //shi
a[2]=n/100%10; //bai
a[3]=n/1000; //qian
int tmp,i,j;
for(i=0;i<3;i++){
for(j=i+1;j<4;j++){
if(a[i]<a[j]){ //a[0] is max
tmp=a[j];
a[j]=a[i];
a[i]=tmp;
}
}
}
return a[0]*1000+a[1]*100+a[2]*10+a[3];
}

int FeiDiJian(int n){
int a[4];
a[0]=n%10; //ge
a[1]=n/10%10; //shi
a[2]=n/100%10; //bai
a[3]=n/1000; //qian
int tmp,i,j;
for(i=0;i for(j=i+1;j if(a[i]>a[j]){
tmp=a[j];
a[j]=a[i];
a[i]=tmp;
}
}
}
return a[0]*1000+a[1]*100+a[2]*10+a[3];
}

int main(){
int n,a[4];
scanf("%d",&n);
a[0]=n%10; //ge

``````a[1]=n/10%10;   //shi
a[2]=n/100%10;  //bai
a[3]=n/1000;    //qian
``````

// if(a[0]==a[1]==a[2]==a[3]){ //debug
if(a[0]==a[1]&&a[1]==a[2]&&a[2]==a[3]){
printf("%d - %d= 0000",n,n);
}
else{
int FDZ=FeiDiZeng(n),FDJ=FeiDiJian(n);

while(FDZ-FDJ!=6174){
const int result=FDZ-FDJ;
printf("%04d - %04d = %04d\n",FDZ,FDJ,result);
FDZ=FeiDiZeng(result);
FDJ=FeiDiJian(result);
}
printf("%04d - %04d = %04d\n",FDZ,FDJ,FDZ-FDJ);

``````}
``````

}

``````
``````
• 点赞
• 写回答
• 关注问题
• 收藏
• 复制链接分享
• 邀请回答

#### 1条回答

ERROR 1:这里代码的for写错了

`````` int FeiDiJian(int n) {
int a[4];
a[0] = n % 10; //ge
a[1] = n / 10 % 10; //shi
a[2] = n / 100 % 10; //bai
a[3] = n / 1000; //qian
int tmp, i, j;
for (i = 0; i < 3;i++ ) {
for (j = i + 1; j < 4; j++) {
if (a[i] > a[j]) {
tmp = a[j];
a[j] = a[i];
a[i] = tmp;
}
}
}
return a[0] * 1000 + a[1] * 100 + a[2] * 10 + a[3];
}
``````

ERROR2：测试点0格式错误：注意等号前面有个空格

`````` if (a[0] == a[1] && a[1] == a[2] && a[2] == a[3]) {
printf("%d - %d = 0000", n, n);
}
``````

这道题把数字当做来处理也比较简单字符串：https://blog.csdn.net/qq_40946921/article/details/81121123

点赞 1 评论 复制链接分享