PAT B1019 / A1069 digital black hole

Given any one of the digits of the four positive integer not identical, if the first four numbers in a non-ascending order, and then in a non-descending order, and then subtract the second number using the first digit, you will get a new digital , repeating to do so, will soon be parked in the "digital black hole," said the 6147, the magic number is also called Kaprekar constant.
For example:
6767
7766-6677 = 1089
9810-0189 = 9621
9621-1269 = 8352
8532-2358 = 6174
 

// digital black hole 
#include <the iostream> 
#include <algorithm> 
the using namespace STD; 

BOOL CMP (A int, int B) 
{ 
	return A> B; // sort descending CMP 
} 

void to_arry (n-int, int NUM []) 
{ 
	for (int I = 0; I <. 4; I ++) // n to each bit of the array which are deposited 
	{ 
		NUM [I] n = 10%; 
		n / = 10; 
	} 
} 

int to_number (int NUM [ ]) // the contents of the array into a digital 
{ 
	int SUM = 0; 
	for (int I = 0; I <. 4; I ++) 
	{ 
		SUM = SUM * 10 + NUM [I];	 
	}	 
	return SUM; 
} 

int main ( ) 
{ 
	// MIN and MAX represent the ascending order and descending order to obtain the maximum and minimum 
	int n-, MIN, MAX; 
	CIN n->>; 
	int NUM [. 5];  
	the while (. 1)
	{
		to_arry(n,num);
		sort(num,num+4);
		MIN=to_number(num);//获取最小值
		sort(num,num+4,cmp);
		MAX=to_number(num);
		n=MAX-MIN;
		printf("%04d-%04d=%04d\n",MAX,MIN,n);
		if(n==0||n==6174) break;	
	} 
	return 0;
}

  

Guess you like

Origin www.cnblogs.com/chuxinbubian/p/11620268.html