卡布列克常數(string類運用)
阿新 • • 發佈:2018-12-03
卡布列克常數
時間限制: 1 Sec 記憶體限制: 128 MB
題目描述
最近,小Q在數學興趣課中瞭解了“卡布列克常數”。卡布列克是一位數學家,他在研究數字時發現:任意一個不是用完全相同數字組成的四位數,如果對它們的每位數字重新排序,組成一個最大的數和一個最小的數,然後用最大數減去最小數,差不夠四位數時補零,類推下去,最後將變成一個固定的數:6174,這就是卡布列克常數。
例如:4321-1234=3087
8730-378=8352
8532-2358=6174
7641-1467=6174
……
小Q想,我能不能程式設計來驗證呢?輸入一個符合條件的四位數,然後驗證運算過程。
輸入
共1行,為任意一個不是用完全相同數字組成的四位數。
輸出
變為卡布列克常數的運算過程,由若干行組成,每行是一個算式,不含空格。
樣例輸入
複製樣例資料
4321
樣例輸出
4321-1234=3087 8730-378=8352 8532-2358=6174
/**/ #include <cstdio> #include <cstring> #include <cmath> #include <cctype> #include <iostream> #include <algorithm> #include <map> #include <set> #include <vector> #include <string> #include <stack> #include <sstream> #include <queue> typedef long long LL; using namespace std; string s; int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); cin >> s; int maxx, minn; while(s != "6174"){ stringstream s1, s2, s3; sort(s.begin(), s.end()); s1 << s, s1 >> minn; reverse(s.begin(), s.end()); //cout << s << endl; s2 << s, s2 >> maxx; int num = maxx - minn; printf("%d-%d=%d\n", maxx, minn, num); s3 << num, s3 >> s; int len = s.size(); for(int i = len; i < 4; i++) s += '0'; } return 0; } /**/