#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool big(int i, int j)
{
return (i > j);
}
bool small(int i, int j)
{
return (i < j);
}
int _tmain(int argc, _TCHAR* argv[])
{
int num;
cin >> num;
do //用do-while保证至少执行一次,防止输入为6174的情况没有输出
{
vector<int> num_temp;
while (num)
{
int temp = num % 10;
num_temp.push_back(temp);
num /= 10;
}
while (num_temp.size()<4) //输入小于1000,则添零补齐四位,无所谓在哪补齐,后面还要排序
{
num_temp.push_back(0);
}
sort(num_temp.begin(), num_temp.end(), big);
int big_num = 0;
for (auto c : num_temp)
{
big_num = big_num * 10 + c;
}
sort(num_temp.begin(), num_temp.end(), small);
int small_num = 0;
for (auto c : num_temp)
{
small_num = small_num * 10 + c;
}
if (big_num==small_num) //如果输入的各位都相等,则输出0000
{
printf("%04d - %04d = 0000\n", big_num, big_num); //输出用printf可以直接补零,用cout还得判断情况手动补零
break;
}
else
{
printf("%04d - %04d = %04d\n", big_num, small_num, big_num - small_num);
}
num = big_num - small_num;
}while (num != 6174);
return 0;
}
1019. 数字黑洞
最新推荐文章于 2020-01-07 21:35:54 发布