我终于做出了第一道!
#include <bits/stdc++.h>
using namespace std;
int biggest_number_4(int n)
{
int a[4], i = 0;
while (n != 0)
{
a[i] = n % 10;
n /= 10;
i ++;
}
for (int i = 0; i < 3; i ++)
{
for (int j = 0; j < 3; j ++)
{
if (a[j] < a[j+1])
{
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
return 1000 * a[0] + 100 * a[1] + 10 * a[2] + a[3];
}
int smallest_number_4(int n)
{
int a[4], i = 0;
while (n != 0)
{
a[i] = n % 10;
n /= 10;
i ++;
}
for (int i = 0; i < 3; i ++)
{
for (int j = 0; j < 3; j ++)
{
if (a[j] > a[j+1])
{
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
return 1000 * a[0] + 100 * a[1] + 10 * a[2] + a[3];
}
int main()
{
int n, num, k;
cin >> n;
k = n;
while (num != 6174)
{
num = biggest_number_4(k) - smallest_number_4(k);
cout << biggest_number_4(k) << "-" << smallest_number_4(k) << "=" << num << endl;
if (num == 6174)
{
break;
}
k = num;
}
}