-
Notifications
You must be signed in to change notification settings - Fork 0
/
1069.cpp
64 lines (52 loc) · 834 Bytes
/
1069.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <cstdio>
#include <algorithm>
using namespace std;
void toArray(int n, int arr[]);
int toNumber(int arr[]);
bool mysort(const int& i1, const int& i2);
int main()
{
int n;
scanf("%04d", &n);
int arr[4];
while (1)
{
toArray(n, arr);
// 递增排序
sort(arr, arr + 4);
int min = toNumber(arr);
// 递减排序
sort(arr, arr + 4, mysort);
int max = toNumber(arr);
n = max - min;
printf("%04d - %04d = %04d\n", max, min, n);
if (n == 0 || n == 6174)
{
break;
}
}
return 0;
}
void toArray(int n, int arr[])
{
for (int i = 0; i < 4; i++)
{
arr[i] = n % 10;
n = n / 10;
}
return;
}
int toNumber(int arr[])
{
int ans = 0;
for (int i = 0; i < 4; i++)
{
ans = ans * 10 + arr[i];
}
return ans;
}
// 降序排列
bool mysort(const int& i1, const int& i2)
{
return i1 > i2;
}