【題目敘述 】https://zerojudge.tw/ShowProblem?problemid=f445 (考生答對率: 17.58%)
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
int a(int x){
vector <int> v;
int ret = 0;
while (x){
v.push_back(x%10);
x /= 10;
}
sort(v.begin(), v.end());
reverse(v.begin(), v.end());
for (int i:v){
ret *= 10;
ret += i;
}
return ret;
}
int b(int x){
vector <int> v;
int ret = 0;
while (x){
v.push_back(x%10);
x /= 10;
}
sort(v.begin(), v.end());
for (int i:v){
ret *= 10;
ret += i;
}
return ret;
}
int main() {
int n;
while (cin >> n){
if (n == 0) break;
cout << "Original number was " << n << "\n";
int cnt = 0;
set <int> st;
while (!st.count(n)){
cnt++;
st.insert(n);
int A = a(n), B = b(n);
n = A-B;
cout << A << " - " << B << " = " << n << "\n";
}
cout << "Chain length " << cnt << "\n\n";
}
}
Python code (credit: Amy Chou)
while True:
n = input()
if n == "0":
break
print(f"Original number was {n}")
num = list()
cnt = 0
while n not in num:
a = ''.join(sorted(n))
b = ''.join(sorted(n, reverse=True))
num.append(n)
n = str(int(b) - int(a))
print(f"{int(b)} - {int(a)} = {n}")
cnt += 1
print(f"Chain length {cnt}")