沒什么難度吧,注意輸入元素幾個特殊的就好了
#include<iostream>#include<string>#include<algorithm>#PRagma warning(disable:4996)using namespace std;int main(){ string str; cin >> str; char temp[10]; sprintf(temp, "%04d", stoi(str));//如果輸入元素不是一個4為的數(shù)字,前面補0 str = temp; if (str == "6174" || str == "0000")//特殊處理一開始就出現(xiàn)6174或0000 { string str1(str), str2(str); sort(str2.begin(), str2.end()); sort(str1.rbegin(), str1.rend()); char temp[10]; sprintf(temp, "%04d", stoi(str1) - stoi(str2)); str = temp; printf("%s - %s = %s/n", str1.c_str(), str2.c_str(), str.c_str()); } while (str != "6174" && str != "0000") { string str1(str), str2(str); sort(str2.begin(), str2.end()); sort(str1.rbegin(), str1.rend()); char temp[10]; sprintf(temp, "%04d", stoi(str1) - stoi(str2)); str = temp; printf("%s - %s = %s/n", str1.c_str(), str2.c_str(), str.c_str()); }}新聞熱點
疑難解答