B1019/A1069 数字黑洞

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了B1019/A1069 数字黑洞相关的知识,希望对你有一定的参考价值。

#include<cstdio>
#include<algorithm>


using namespace std;

bool cmp1(int a, int b)
{
	return a>b;//sorted by decreasing
}

bool cmp2(int a, int b)
{
	return a<b;//sorted by decreasing
}


void to_array(int n, int arr[])
{
	for (int i = 0; i<4; i++)
	{
		arr[i] = n % 10;//this is a skill
		n = n / 10;
	}
}

int to_number(int arr[])
{
	int sum = 0;
	for (int i = 0; i<4; i++)
	{
		sum = sum * 10 + arr[i];//this is a skill
	}

	return sum;
}

int main()
{
	int n;
	int max, min;
	int num[5];

	scanf("%d",&n);

	while (1)//a way always do something
	{
		to_array(n, num);
		sort(num, num + 4, cmp1);//its great, meet your need, can up and down
		max = to_number(num);
		sort(num, num + 4, cmp2);
		min = to_number(num);
		n = max - min;
		printf("%04d - %04d = %04d\n", max, min, n);
		if (n == 0 || n == 6174)
			break;
	}
	return 0;
}

 

以上是关于B1019/A1069 数字黑洞的主要内容,如果未能解决你的问题,请参考以下文章

1019 数字黑洞 (20分)/1069 The Black Hole of Numbers (20分)

PATB1019 数字黑洞 (20)(20 分)

1019 数字黑洞 (20 分)

PAT乙级 1019. 数字黑洞 (20)

1019. 数字黑洞 (20)

1019 数字黑洞