Looking for a four-digit "digital black hole"

Looking for a four-digit "digital black hole"

Digital black hole

"Mathematical Black Hole":

Any 4-digit natural number, rearrange the digits that make up the number to form a maximum number and a minimum number,

After the two numbers are subtracted, the difference is still a natural number.

Repeat the above calculations and you will find a mysterious number.

description

Input:
a 4-digit natural number.

Output:
Starting from the 4-digit natural number, the natural number transformed according to the meaning of the question each time until the mathematical black hole.

Case

Input sample:
7700
output sample:

7700
7623
5265
3996
6264
4176
6174

The c++ code is as follows

#include <iostream>
using namespace std;
int main()
{
    
    
	int n, a, b, c, d;
	cin >> n;
	cout << n << endl;
	while(n != 6174)
	{
    
    
		a = n / 1000;
		b = n / 100 % 10;
		c = n / 10 % 10;
		d = n % 10;
		if(a < b)
			a ^= b ^= a ^= b;
		if(a < c)
			a ^= c ^= a ^= c;
		if(a < d)
			a ^= d ^= a ^= d;
		if(b < c)
			b ^= c ^= b ^= c;
		if(b < d)
			b ^= d ^= b ^= d;
		if(c < d)
			c ^= d ^= c ^= d;
		n = a * 1000 + b * 100 + c * 10 + d -
			(a + b * 10 + c * 100 + d * 1000);
		if(n < 10)
			cout << "000" << n << endl;
		else if(n < 100)
			cout << "00" << n << endl;
		else if(n < 1000)
			cout << "0" << n << endl;
		else
			cout << n << endl;
		}
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_51808107/article/details/109792892