#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int make_big(int n)
{
int ary[4];
ary[0] = n / 1000;
ary[1] = n / 100 % 10;
ary[2] = n / 10 % 10;
ary[3] = n % 10;
sort(ary, ary + 4);
int result = 0;
for (int i = 3; i >= 0; i--)
{
result *= 10;
result += ary[i];
}
return result;
}
int make_small(int n)
{
int ary[4];
ary[0] = n / 1000;
ary[1] = n / 100 % 10;
ary[2] = n / 10 % 10;
ary[3] = n % 10;
sort(ary, ary + 4);
int result = 0;
for (int i = 0; i < 4; i++)
{
result *= 10;
result += ary[i];
}
return result;
}
int main()
{
int n;
cin >> n;
int big = make_big(n);
int small = make_small(n);
if (big == small)
{
printf("%04d - %04d = 0000\n", big, small);
}
else
{
while (big - small != 6174)
{
int result = big - small;
printf("%04d - %04d = %04d\n", big, small, result);
big = make_big(result);
small = make_small(result);
}
printf("%04d - %04d = 6174\n", big, small);
}
}PAT (Advanced) 1069. The Black Hole of Numbers (20)
最新推荐文章于 2020-02-15 16:03:43 发布
本文介绍了一个关于6174数的猜想实现过程。通过将四位数的各个位进行排序,形成最大和最小的四位数,并计算两者之差,最终达到6174这个神奇的数字。该过程涉及到了C++语言的基础知识,如数组操作、排序算法等。
877

被折叠的 条评论
为什么被折叠?



