简单数字问题之数字黑洞问题

#include<algorithm>
#include <iostream>
using namespace std;

void divide(int a, int b[] )
{
	for (int i = 0; i < 4; i++)
	{
		b[i] = a % 10;
		a /= 10;
	}
}
bool cmp1(int a, int b) 
{
	return a > b;

}

bool cmp2(int a, int b)
{
	return a<b;
}
int  sort1(int c[]) 
{
	int a = 0;
	int pow = 1;	
	//从大到小
	sort(c, c + 4, cmp1);
	
	for (int i = 3; i >=0; --i)
	{
		a += c[i] * pow;
		pow *= 10;
	}
	return a;
}

int sort2( int c[])
{
	int b = 0;
	sort(c, c + 4, cmp1);
	int pow = 1;
	for (int i = 0; i < 4; i++)
	{
		b += c[i] * pow;
		pow *= 10;
	}
	return b;
}

int a = 0; int  c = 0;
int b[4] = {};
int main()
{
	int n;
	cin >> n;
	divide(n, b);

	int flag = 0;
	for (int i = 1; i < 4; i++)
	{
		if (b[i - 1] != b[i])
		{
			flag = 1;
		}
	}
	if (flag == 0) {
		printf("%04d", flag);  return 0;
	}

	while (true) {
		a = sort1(b);
		c = sort2(b);
		cout << a << "-" << c << "=" << a - c << endl;
		if (a - c != 6174)
		{
			divide(a - c, b);
		}
		else break;
	}
	return 0;
}