-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1019.cpp
55 lines (51 loc) · 1.03 KB
/
1019.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
#include <iostream>
#include <iomanip>
using namespace std;
void judge( int num);
int increase( int num);
int decrease( int num);
int main() {
int n;
cin >> n;
judge( n );
return 0;
}
void judge( int num) {
int a = increase( num);
int b = decrease( num);
cout << setfill('0') << setw(4) << b << "-"
<< setfill('0') << setw(4) << a << "="
<< setfill('0') << setw(4) << b-a << endl;
if( (b-a) == 0 || (b-a) == 6174) return;
judge(b-a);
}
int increase( int num) {
int a[4];
for( int i=0; i<4; ++i) {
a[i] = num%10;
num /= 10;
}
for( int i=0; i<3; ++i)
for( int j=0; j<3-i; ++j)
if( a[j] > a[j+1]) {
int t = a[j];
a[j] = a[j+1];
a[j+1]= t;
}
return ( a[0] * 1000 + a[1] * 100 + a[2] * 10 + a[3] );
}
int decrease( int num) {
int a[4];
for( int i=0; i<4; ++i) {
a[i] = num%10;
num /= 10;
}
for( int i=0; i<3; ++i)
for( int j=0; j<3-i; ++j)
if( a[j] < a[j+1]) {
int t = a[j];
a[j] = a[j+1];
a[j+1]= t;
}
return ( a[0] * 1000 + a[1] * 100 + a[2] * 10 + a[3] );
}