7-44 number of black holes (20 points)

The number of black holes is also known as the trap number, also known as "Kaprekar problem", which is a kind of number with peculiar conversion characteristics.

Any three digits with different digits will get 495 after a limited number of "rearrangement and difference" operations. The final 495 is the number of three black holes. The so-called "rearrange difference" operation is the maximum number after rearrangement minus the minimum number after rearrangement. (6174 is the number of four black holes.)

For example, for three digits 207:

  • In the first rearrangement, 720 - 27 = 693;
  • The second rearrangement results in: 963 - 369 = 594;
  • The third rearrangement result is: 954 - 459 = 495;

In the future, it will stay at 495. If the three digits of three digits are all the same, it will be 0 after one conversion.

Arbitrary input a three digit, programming gives the process of rearrangement.

Input format:

Enter to give a three digit number on a single line.

Output format:

Output the process of rearranging the difference as follows:

No.: maximum number after number rearrangement - minimum number after rearrangement = difference

 

The sequence number starts at 1 until 495 appears to the right of the equal sign.

Input example:

123

Output example:

1: 321 - 123 = 198
2: 981 - 189 = 792
3: 972 - 279 = 693
4: 963 - 369 = 594
5: 954 - 459 = 495

 

 1 #include <stdio.h>
 2 
 3 int number(int a[], int len, int flag);
 4 int max(int a[], int len);
 5 void dd(int a[], int n);
 6 
 7 int main(int argc, char const *argv[])
 8 {
 9     int n;
10     int a[3];
11 
12     scanf("%d", &n);
13 
14     int len = sizeof(a)/sizeof(a[0]);
15     
16     // The number of inputs is 495, and it needs to be output once
17     for ( int i = 1; n != 495 || i == 1; i++ ) {
18         dd(a,n);
19         //    Selection sort
20         for ( int i = len-1; i > 0; i-- ) {
21             int maxid = max(a,i+1);
22             // swap a[maxid], a[len-1]
23             int t = a[maxid];
24             a[maxid] = a[i];
25             a[i] = t;
26         }
27         // 1-->mim 0-->max
28         int min = number(a,sizeof(a)/sizeof(a[0]),1);
29         int max = number(a,sizeof(a)/sizeof(a[0]),0);
30         if ( min == max ) {    // Are the three numbers of three digits the same
31             printf("%d: %d - %d = %d\n", i, max, min, max-min);
32             break;
33         } else {
34             printf("%d: %d - %d = %d\n", i, max, min, max-min);
35         }
36         n = max - min;
37     }
38     
39     return 0;
40 }
41 
42 int number(int a[], int len, int flag)
43 {
44     int num = 0;
45     if ( flag ) {
46         for ( int i = 0; i < len; i++ ) {
47             num = num * 10 + a[i];
48         }
49     } else {
50         for ( int i = len-1; i >= 0; i-- ) {
51             num = num * 10 + a[i];
52         }
53     }
54     return num;
55 }
56 
57 int max(int a[], int len)
58 {
59     int maxid = 0;
60     int i;
61     for ( i = 1; i < len; i++ ) {
62         if ( a[i] > a[maxid] ) {
63             maxid = i;
64         }
65     }
66     return maxid;
67 }
68 
69 void dd(int a[], int n)
70 {
71     a[0] = n / 100;
72     a[1] = n / 10 % 10;
73     a[2] = n % 10;
74 }

Tags: C Programming

Posted on Thu, 13 Feb 2020 13:06:17 -0500 by Chris16962