1019 Digital Black Hole (20 points)-Test Point 5

1019 Digital Black Hole (20 points)

Given any 4-digit positive integer whose digits are not exactly the same, if we first sort the 4 digits in non-increasing order, then sort them in non-decreasing order, and then subtract the second digit from the first digit, we will get a new digital. Repeatedly doing this, we will soon stop at 6174, known as the "digital black hole", this magical number is also called Kaprekar's constant.

For example, if we start from 6767, we will get

7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
7641 - 1467 = 6174
... ...

Now given any 4-digit positive integer, please write a program to demonstrate the process of reaching a black hole.

Input format:
input gives one (0, 1 0 4) (0,10^4)(0,104 )A positive integer N in the interval.

Output format:
If the 4 digits of N are all equal, output N-N = 0000 in one line; otherwise, output each step of the calculation in one line until 6174 appears as a difference. See the example for the output format. Note that each number is output in a 4-digit format.

Input example 1:

6767

Output sample 1:

7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174

Input example 2:

2222

Output sample 2:

2222 - 2222 = 0000

answer

The meaning of this question is to calculate the four-digit number back and forth to separate each number and then combine the maximum value minus the minimum value, and loop until the same two numbers have a difference of 0 or a difference of 6174.
After sorting to find the largest number, the smallest value can be reversed.
The big function splits the four digits, and then sorts them in buckets. Starting with the total big number, the new four-digit maximum number
is combined. The small function takes the reverse number of the return value of the big function.
Note: If the test point 5 is abnormal, it means that the input n=6174 is not considered, and the output should be:

7641 - 1467 = 6174

Instead of a null value, because of the difference.


AC routine

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<map>
#include<algorithm>
#include<set>
using namespace std;

int big(int n)
{
    
    
    int a=n/1000,b=n%1000/100,c=n%100/10,d=n%10;
    int m[10];memset(m,0,sizeof(m));
    int sum=0;
    m[a]++;m[b]++;m[c]++;m[d]++;
    for(int i=9;i>=0;)
    {
    
    
        if(m[i]>1){
    
    sum=sum*10+i;m[i]--;}
        else if(m[i]==1){
    
    sum=sum*10+i;i--;}
        else i--;
    }
    return sum;
}
int small(int n)
{
    
    
    return n/1000+n%1000/100*10+n%100/10*100+n%10*1000;
}
int main()
{
    
    
#ifdef ONLINE_JUDGE
#else
    freopen("in.txt","r",stdin);
#endif
int n,a,b;
cin>>n;
if(n==6174)
    {
    
    a=big(n);b=small(a);n=a-b;
    printf("%04d - %04d = %04d\n",a,b,n);}
while(n!=0&&n!=6174)
{
    
    
    a=big(n);b=small(a);n=a-b;
    printf("%04d - %04d = %04d\n",a,b,n);
}

return 0;
}

Guess you like

Origin blog.csdn.net/qq_41962612/article/details/114683870