pat1019:数字黑洞

https://www.patest.cn/contests/pat-b-practise/1019

#include "stdio.h"

void swap(int *c, int *d)
{
	int temp;
	temp = *c;
	*c = *d;
	*d = temp;
}
void kap(int k)
{
	int i, j, max, min;
	int a[4] = {k/1000, (k%1000)/100, (k%100)/10, k%10};
	if((a[0] == a[1]) && (a[1] == a[2]) && (a[2] == a[3]))
	{
		printf("%d - %d = 0000\n", k, k);
		return ;
	}
	for(i = 0; i < 4; ++i)
		for(j = i+1; j < 4; ++j)
			if(a[i] < a[j])
				swap(&a[i], &a[j]);
	max = a[0] * 1000 + a[1] * 100 + a[2] * 10 + a[3];
	min = a[3] * 1000 + a[2] * 100 + a[1] * 10 + a[0];
	printf("%04d - %04d = %04d\n", max, min, max - min);
	if(6174 != max - min)
		kap(max - min);
}
int main()
{
	int k;
	scanf("%d", &k);
	kap(k);

	return 0;
}


你可能感兴趣的:(c,pat)