标签:cout 数字 code iostream turn str ace style 结果
// 这道题运用的小技巧很多
#include <iostream> #include <algorithm> #include <cstring> using namespace std; bool cmp (char a, char b) { // 用来递减排序 return a > b; } int main() { string num; cin >> num; num.insert(0, 4 - num.size(), ‘0‘); do { string a = num, b = num; sort(b.begin(), b.end()); sort(a.begin(), a.end(), cmp); int result = stoi(a) - stoi(b); // 将字符串转化成数字 num = to_string(result); // 将数字转换成字符串,(其实可以是任意两个类型进行转换,很灵活); num.insert(0, 4 - num.size(), ‘0‘); // 如果结果不足4位,进行补零 cout << a << " - " << b << " = " << num << endl; } while (num != "6174" && num != "0000"); return 0; }
标签:cout 数字 code iostream turn str ace style 结果
原文地址:https://www.cnblogs.com/Hk456/p/10752708.html