【PAT甲级】1069 The Black Hole of Numbers (20 分)

题意:

输入一个四位的正整数N,输出每位数字降序排序后的四位数字减去升序排序后的四位数字等于的四位数字,如果数字全部相同或者结果为6174(黑洞循环数字)则停止。

trick:

这道题一反常态的输入的数字是一个int类型而不是包含前导零往常采用字符串的形式输入,所以在测试点2,3,4如果用字符串输入会超时。。。。。

AAAAAccepted code:

 1 #define HAVE_STRUCT_TIMESPEC
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 int n;
 5 char x[7],y[7];
 6 int main(){
 7     scanf("%d",&n);
 8     x[0]=n/1000+'0';
 9     n%=1000;
10     x[1]=n/100+'0';
11     n%=100;
12     x[2]=n/10+'0';
13     n%=10;
14     x[3]=n+'0';
15     while(1){
16         if(x[0]==x[1]&&x[1]==x[2]&&x[2]==x[3]){
17             printf("%s - %s = 0000",x,x);
18             return 0;
19         }
20         sort(x,x+4);
21         y[0]=x[3];
22         y[1]=x[2];
23         y[2]=x[1];
24         y[3]=x[0];
25         int xx=(x[0]-'0')*1000+(x[1]-'0')*100+(x[2]-'0')*10+x[3]-'0';
26         int yy=(y[0]-'0')*1000+(y[1]-'0')*100+(y[2]-'0')*10+y[3]-'0';
27         int zz=yy-xx;
28         printf("%04d - %04d = %04d",yy,xx,zz);
29         if(zz==6174)
30             break;
31         else
32             printf("\n");
33         x[0]=zz/1000+'0';
34         zz%=1000;
35         x[1]=zz/100+'0';
36         zz%=100;
37         x[2]=zz/10+'0';
38         zz%=10;
39         x[3]=zz+'0';
40     }
41     return 0;
42 }

 

 

 

posted @ 2019-11-03 02:50  sewage  阅读(303)  评论(0编辑  收藏  举报