poj 1350 Cabric Number Problem

#include <iostream>
#include
<algorithm>
#include
<vector>
#include
<string>
#include
<math.h>
using namespace std;
int to_int(vector<char> coll)
{
int sum=0;
for(int i=0;i<coll.size();i++)
sum
+=(coll[i]-48)*pow(10.0,i);
return sum;
}
void to_vec(vector<char>& coll,int n)
{
while(n)
{
coll.push_back(
char(n%10+48));
n
=n/10;
}
}
int main()
{
string data;int i,max,min,dis,t;
while(cin>>data&&data!="-1")
{
cout
<<"N="<<data<<":\n";
if(data.size()!=4||data.find_first_not_of(data[0])==string::npos)
{
printf(
"No!!\n");
continue;
}

t
=0;
vector
<char> digit;
for(i=0;i<data.size();i++)
digit.push_back(data[i]);
do
{
sort(digit.begin(),digit.end());
max
=to_int(digit);
reverse(digit.begin(),digit.end());
min
=to_int(digit);
dis
=max-min;
printf(
"%d-%d=%d\n",max,min,dis);
t
++;
digit.clear();
to_vec(digit,dis);
}
while (dis!=0&&dis!=6174);

printf(
"Ok!! %d times\n",t);
}
return 0;
}

  

posted on 2011-07-20 22:39  sysu_mjc  阅读(134)  评论(0编辑  收藏  举报

导航