码迷,mamicode.com
首页 > 其他好文 > 详细

B1019/A1069 数字黑洞

时间:2017-10-10 23:24:09      阅读:362      评论:0      收藏:0      [点我收藏+]

标签:can   some   log   break   skill   nbsp   using   algorithm   ++   

#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 数字黑洞

标签:can   some   log   break   skill   nbsp   using   algorithm   ++   

原文地址:http://www.cnblogs.com/dusanlang/p/7648162.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!