Java implements digital black hole

Given any 4-digit positive integer whose digits are not exactly the same, if we first sort the 4 numbers by non-increasing, then sort by non-decreasing, and then subtract the second number by the first number, we will get a new digital. Repeating this all the time, we will soon stop at 6174, known as the "digital black hole". This magic number is also called the Kaprekar constant.

For example, starting 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 the black hole.

Input format:
Enter a given (0,10
4
) positive integer in the range N.

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

Input sample 1:
6767

Sample output 1:
7766-6677 = 1089
9810-0189 = 9621
9621-1269 = 8352
8532-2358 = 6174

Input example 2:
2222

Sample output 2:
2222-2222 = 0000

I saw some relatively easy-to-understand methods on the Internet. The easier part of this question is that the number of digits has been fixed, so you can use some brainless methods. The small detail is that there should be a test data that is only executed once and obtained 6174 So the loop chooses do-while to achieve, there is also to use the% 0d of printf to fill the front with 0

The following is the code: `Insert here import java.util.Arrays;
import java.util.Scanner;
public class Main {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    
    int n = input.nextInt();
    int result = numHighSort(n) - numLowSort(n);
    if(result == 0)
        System.out.printf("%04d - %04d = %04d\n",numHighSort(n),numLowSort(n),result);
    else
       {
        do
        {
            result = numHighSort(n) - numLowSort(n); //改变result
            System.out.printf("%04d - %04d = %04d\n",numHighSort(n),numLowSort(n),result);
            n = result;//改变n
        }while(n != 6174);       
       }
    
}
public static int numHighSort(int i)
{
    int[] number = new int[4];
    number[0] = i / 1000;
    number[1] = i % 1000 /100;
    number[2] = i % 100 / 10;
    number[3] = i % 10;
    Arrays.sort(number);//将数组元素升序排放
    
    return number[3]*1000 + number[2] * 100 + number[1]*10 + number[0]; 
}
public static int numLowSort(int i)
{
    int[] number = new int[4];
    number[0] = i / 1000;
    number[1] = i % 1000 /100;
    number[2] = i % 100 / 10;
    number[3] = i % 10;
    
    Arrays.sort(number);
    
    return number[0]*1000 + number[1] * 100 + number[2]*10 + number[3]; 
}

}
I hope to be helpful to everyone

Published 3 original articles · praised 3 · visits 27

Guess you like

Origin blog.csdn.net/weixin_45834063/article/details/105413496