AC代码
1 #include <stdio.h> 2 #include <string.h> 3 int cmpmin (const void *a,const void *b) 4 { 5 return *(int*)a - *(int*)b; 6 } 7 int cmpmax (const void *a1,const void *b1) 8 { 9 return *(int*)b1 - *(int*)a1; 10 } 11 /* 12 int cmp1 (const void *a,const void *b) 13 { 14 return *(int*)a-*(int*)b; 15 } 16 int cmp2 (const void *a1,const void *b1) 17 { 18 return *(int*)b1-*(int*)a1; 19 } 20 */ 21 /* 22 void Minsort(char *a,int size) 23 { 24 int i = 0 ; 25 char temp = 0; 26 int j; 27 for(j = 1;j < size;j++) 28 for(i = 0;i < size - j;i++) 29 { 30 if((a[i] - '0') > a[i+1] - '0') 31 { 32 temp = a[i]; 33 a[i] = a[i+1]; 34 a[i+1] = temp; 35 } 36 } 37 } 38 void Maxsort(char *a,int size) 39 { 40 int i = 0 ; 41 char temp = 0; 42 int j; 43 for(j = 0;j < size-1;j++) 44 for(i = 0;i < size - j;i++) 45 { 46 if((a[i] - '0') < a[i+1] - '0') 47 { 48 temp = a[i]; 49 a[i] = a[i+1]; 50 a[i+1] = temp; 51 } 52 } 53 } 54 */ 55 int main () 56 { 57 int input; 58 scanf("%d",&input); 59 int box[4]; 60 int ret = input; 61 if(ret % 1111 == 0) 62 { 63 printf("%d - %d = 0000",ret,ret); 64 } 65 else 66 if(ret == 6174) 67 { 68 printf("7641 - 1467 = 6174"); 69 } 70 while(ret != 6174 &&(ret % 1111)!= 0) 71 { 72 int i; 73 int mask = 1000; 74 for(i = 0;i < 4;i++) 75 { 76 box[i] = ret / mask; 77 ret = ret % mask; 78 mask = mask / 10; 79 } 80 qsort(box,4,sizeof(box[0]),cmpmax); 81 int max= 0; 82 mask = 1000; 83 for( i = 0;i < 4;i++) 84 { 85 max += mask * box[i]; 86 mask = mask / 10; 87 } 88 qsort(box,4,sizeof(box[0]),cmpmin); 89 int min = 0; 90 mask = 1000; 91 for(i = 0;i < 4;i++) 92 { 93 min += mask * box[i]; 94 mask = mask / 10; 95 } 96 ret = max - min; 97 printf("%04d - %04d = %04d ",max,min,ret); 98 } 99 }