// This question has many traps and has contributed two Ole and one wa! Ah! Trap: Make sure that the number of inputs must be 4 bits, and the number cannot be greater than or less than 4 bits, or Ole! The output is no, OK, not no. OK # include <iostream> # include <string> # include <sstream> # include <algorithm> using namespace STD; bool CMP (char, char B) {return A> B;} int main () {int N, C, temp1, temp2, ans; string STR, str1, str2; bool flag, flag1, flag2; while (CIN> N & n! =-1) {cout <"n =" <n <":" <Endl; C = 0; flag = flag1 = flag2 = false; while (1) {stringstream stream; stream <n; stream> STR; If (! Flag2) // determines whether the input number is 4 digits. You can skip this step for the second time without further judgment! {Flag2 = true; If (Str. Length ()! = 4) {flag = true; break ;}} str1 = str2 = STR; sort (str1.begin (), str1.end (); sort (str2.begin (), str2.end (), CMP); If (! Flag1) {If (str1 = str2) {flag = true; break ;}} stringstream stream1; stream1 <str1; stream1> temp1; stringstream stream2; stream2 <str2; stream2> temp2; ans = temp2-temp1; C ++; flag1 = true; cout <temp2 <"-" <temp1 <"=" <ans <Endl; If (ANS = 6174 | ans = 0) break; else n = ans;} If (FLAG) cout <"No !! "<Endl; else cout <" OK !! "<C <" times "<Endl;} system (" pause ");}