given any number of 4-bit positive integers that are not exactly the same numbers, we get a new number if we first order 4 numbers in increments, then in non descending order, then subtract the 2nd number with the 1th digit.
To repeat this, we will soon stop at 6174, which is called the "digital Black Hole", which is also called the Kaprekar constant.
For example, starting with 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 integer, please write a program to demonstrate the process of reaching the black hole.
Input format: Enter a positive integer n within a (0, 10000) interval. Output format: If N's 4-bit numbers are all equal, output "n-n = 0000" In one line, otherwise each step of the calculation is output in one line until 6174 is shown as the difference, and the output format is shown in the sample.
Note that each digit is output in 4-digit format.
Input Sample Example 1:6,767 Output Sample 1:7766-6677 = 1089 9810-0189 = 9621 9621-1269 = 8352 8532-2358 = 6174 Input Sample 2: 2222 Output Sample 2:2222-2222 = 0000
#include <iostream> #include <algorithm> #include <string.h> #include <math.h> using namespace s
td
int sub;
Char a[4];
Char b[4];
int main () {int m=0,n=0;
cin>>a;
int X=strlen (a);
if (x<4) {for (int i=x;i<4;i++) {a[i]= ' 0 ';
} int t=0;
while (t!=6174) {sort (a,a+4);
for (int i=0;i<4;i++) {b[i]=a[3-i];
for (int i=0;i<4;i++) {m=m*10;
n=n*10;
m+= (a[i]-' 0 ');
n+= (b[i]-' 0 ');
} sub=n-m;
T=sub;
if (sub==0) {break;
printf ("%04d-%04d =%04d\n", n,m,sub);
m=0;n=0;
for (int i=3;i>=0;i--) {a[i]=sub%10+ ' 0 ';
sub/=10;
} if (t==0) {printf ("%04d-%04d = 0000", n,m);
cout<<n<< "-" <<m<< "= 0000"; return 0; }