Given any of the 4-bit positive integers where the numbers are not exactly the same, if we first put 4 numbers in a non-ascending order, then the non-descending sort, and then the 1th number minus the 2nd number, we will get
A new number. To repeat this, we will soon stop at 6174 of the "digital black Hole", the magic number 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-bit positive integers, write a program to demonstrate the process of reaching the black hole.
Input Description:
The input gives a positive integer n in the interval (0, 10000).
Output Description:
If the 4-bit numbers of N are all equal, output "n-n = 0000" In a row, otherwise each step of the calculation is output in a row until 6174 is the difference, and the output format is shown in the sample. Note that each number is output in 4-digit format.
Input Example:
6767
Output Example:
7766-6677 = 10899810-0189 = 96219621-1269 = 83528532-2358 = 6174
import java.util.scanner;import java.util.arrays;public class main { public static void main (String[] args) { scanner in = new scanner (system.in); string num = in.next (); int Result = 0; while (result!=6174) { int left = 0; int right = 0; char[] temp = creat (num); char[] l = new char[4]; arrays.sort (temp); for (int i = 3;i>=0;i--) { left = left*10+temp[i]-' 0 '; right = right*10+temp[3-i]-' 0 '; l[i] = temp[3-i] ; } if (left==right) { system.out.println (New string (L) + " - " +new String (temp) + " = 0000"); return; } result = left-right; system.out.println (New string (L) + " - " +new string (temp) + " = "+result"); num = "" +result; } } private static char[] creat (String s) { char[] num = new char[4]; for (int i = 0;i<num.length;i++) { if (I>=s.length ()) num[i] = ' 0 '; else num[i] = s.charat (i); } return num; } }
NetEase 2017 school recruit inside push the written test practice volume programming problem 1--1019. Digital Black Hole 20