当前位置:网站首页>Pat class B 1019 C language

Pat class B 1019 C language

2022-06-23 05:57:00 Octopus bro

1019. Digital black hole (20)


Given any number that is not exactly the same 4 Positive integer , If we put 4 Numbers are sorted in non incremental order , And then in non decreasing order , Then use the 1 A number minus 2 A digital , Will get a new number . Keep doing this again and again , We will soon stop having “ Digital black hole ” It's called. 6174, This magic number is also called Kaprekar constant .

for example , We from 6767 Start , Will get

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

Any 4 Positive integer , Please write a program to demonstrate the process of reaching the black hole .

Input format :

Type to give a (0, 10000) A positive integer in an interval N.

Output format :

If N Of 4 The digits are all equal , Then output... In one line “N - N = 0000”; Otherwise, each step of the calculation will be output in one line , until 6174 Appear as a difference , See the example for the output format . Pay attention to each number press 4 Bit format output .

sample input 1:
6767
sample output 1:
7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
sample input 2:
2222
sample output 2:
2222 - 2222 = 0000


Ideas : The idea of this question is to break up a four digit input , Store an array of four in length , Sort in ascending and descending order respectively , Then subtract the number of ascending sort from the number of descending sort , Judge the result , If it is not 6174, Recursively call the difference as a parameter

One 、 Starting variable

1.N, Accept a four digit positive integer

Two 、 operation 、

1. Break up the four digits and store them in the array num

2. Incremental sort

3. Descending sort

4. Input subtraction process and result , If it is a number with the same four digits or the result of subtraction is 6174 Then output , Otherwise, the subtraction result is called recursively as a parameter

3、 ... and 、 Code

#include "stdio.h"
#include "stdlib.h" 
void Insperate(int a, int * num);// Divide the input number into four numbers and store them in the array  
int Increase(int a);// Sort the four numbers incrementally  
int Decrease(int a);// Sort the four numbers in descending order  
void PrintRes(int a);// Output the results according to the requirements of the topic  
int cmp1(const void * a, const void * b);//qsort Incremental sort  
int cmp2(const void * a, const void * b);//qsort Descending sort 
int main()
{
    int N;
    scanf("%d",&N);
    PrintRes(N);
    return 0;
}
int cmp1(const void * a, const void * b)//qsort Incremental sort  
{
    return *(int *)a - *(int *)b;
} 
int cmp2(const void * a, const void * b)//qsort Descending sort 
{
    return *(int *)b - *(int *)a;
}
void Insperate(int a,int * num)
{
    int i = 0;
    for(i = 0; i < 4; i++)
    {
        num[i] = a % 10;
        a /= 10;
    }
}
int Increase(int a)
{
    int ret;
    int num[4];
    Insperate(a, num);
    qsort(num,4,sizeof(num[0]),cmp1);// Use quick sort incremental sort  
    ret = num[0] * 1000 + num[1] * 100 + num[2] * 10 +num[3];// Compose a four digit number  
    return ret;
}
int Decrease(int a)
{
    int ret;
    int num[4];
    Insperate(a, num);
    qsort(num,4,sizeof(num[0]),cmp2);// Use fast descending sort 
    ret = num[0] * 1000 + num[1] * 100 + num[2] * 10 +num[3];
    return ret;
}

void PrintRes(int a)
{
    int inc,dec;
    inc = Increase(a);
    dec = Decrease(a);
    int sub = dec - inc;
    if(sub == 0)// Recursive exit one  
    {
        printf("%4d - %4d = 0000", dec, inc);// If it's four identical numbers , Direct output  
        return;
    }
    else if(sub == 6174)// Recursive exit 2  
    {
        printf("%04d - %04d = %04d",dec,inc,sub);
        return;
    }
    else
    {
        printf("%04d - %04d = %04d",dec,inc,sub);
        printf("\n");
        PrintRes(sub);// Make a recursive call to the result  
    }
}


原网站

版权声明
本文为[Octopus bro]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230410347822.html