[刷题] PTA 7-44 黑洞数

我的程序,一个用例通不过

 1 #include<stdio.h>
 2 void sort(int *a,int n);
 3 
 4 int main() {
 5     int num,a,b,c,count=1;
 6     scanf("%d",&num);
 7     while(num!=495&&num!=0) {
 8         a = num/100;
 9         b = num%100/10;
10         c = num%100%10;
11         int p[] = {a,b,c};
12         sort(p,3);
13         int g1,g2;
14         g1 = 100*p[0]+10*p[1]+p[2];
15         g2 = 100*p[2]+10*p[1]+p[0];
16         num = g1-g2;
17         printf("%d: %d - %d = %d\n",count,g1,g2,num);
18         count++;
19     }
20 }
21 
22 void sort(int *a,int n) {
23     int i, j, t;
24     for (i = 1; i < n; i++) {
25         t = a[i];
26         j = i - 1;
27         while (j >= 0 && t > a[j]) {
28             a[j+1] = a[j];
29             j--;
30         }
31         a[j+1] = t;
32     }
33 }

网友的程序,更加简洁:

 1 #include <stdio.h>
 2 int main(){
 3 int n,max,min,f=0,a,b,c,t;
 4 scanf("%d",&n);
 5 while(n!=495||f==0){
 6 a=n/100;b=n/10%10;c=n%10;
 7 if (a<b){
 8 t = a;
 9 a = b;
10 b = t;
11 }
12 if (a<c){
13 t = a;
14 a = c;
15 c = t;
16 }
17 if (b<c){
18 t = b;
19 b = c;
20 c = t;
21 }
22 max = a*100+b*10+c;
23 min = c*100+b*10+a;
24 n = max - min;
25 f++;
26 printf("%d: %d - %d = %d\n",f,max,min,n);
27 }
28 
29 return 0;
30 }

 

posted @ 2019-04-23 09:46  cxc1357  阅读(445)  评论(0编辑  收藏  举报