PAT1019 digital black hole

#include <iostream>
#include <string>
#include <algorithm>
#include <sstream>

using namespace std;

int2string String (int ival is)
{
the stringstream Stream; // Create the stringstream a, i.e., the input may be used also be used to output data
stream << ival; // int type value will pass the stringstream,
return stream.str (); / / stringstream member str () function returns a character string corresponding
}
int Stoi (string S)
{
the stringstream Stream;
Stream << S;
int T;
Stream >> T;
return T;
}
int main ()
{
string num_str;
>> num_str CIN;
for (int I = num_str.size (); I <. 4; I ++)
num_str + = '0';


the while (to true)
{
// check if all the character as digital
IF (num_str.find_first_not_of (num_str [0]) == :: String NPoS)
{
COUT num_str << << "-" << << num_str "=" << "0000" << endl;
BREAK; // end loop
}
// numeric characters in ascending order
Sort (num_str.begin (), num_str.end ());
int = num_ascend Stoi (num_str);
// descending numeric characters Sort
Reverse (num_str.begin (), num_str.end ());
int = num_descent Stoi (num_str);
IF (! = num_ascend num_descent) // case where not all the same four-digit
{
IF (num_descent - num_ascend == 6174)
{
<< << num_descent COUT "-" << << num_ascend "="
<< endl << 6174;
break; // end loop
}
the else // difference is not 6174
{
int2string num_ascend_str = String (num_ascend);
IF (num_ascend_str.size () =. 4!) // output control format, there must be filled up with four
num_ascend_str.insert (0, 4 - num_ascend_str.size ( ), '0') ;
COUT num_descent << << "-" << << num_ascend_str "="
<< num_descent - num_ascend << '\ n-';
num_str = int2string (num_descent - num_ascend);
}
}
}
getchar ();
getchar ();
0 return;
}

Guess you like

Origin www.cnblogs.com/zxzmnh/p/11627056.html