#include<iostream>
#include<vector>
#include<algorithm>
#include<utility>
using namespace std;
int calcMax(vector<int> v, int validNum){
//0 1 2 3
if(validNum==0){
return 0;
}else if(validNum==1){
return v[3];
}else if(validNum==2){
return v[3]*10+v[2];
}else if(validNum==3){
return v[3]*100+v[2]*10+v[1];
}else{
return v[3]*1000+v[2]*100+v[1]*10+v[0];
}
}
pair<int,int> maxMin(int num){
vector<int> v(4);
int validNum=4;
v[0]=num/1000;
v[1]=(num-v[0]*1000)/100;
v[2]=(num-v[0]*1000-v[1]*100)/10;
v[3]=num-v[0]*1000-v[1]*100-v[2]*10;
if(v[0]==0){
validNum--;
}
if(v[0]==0 && v[1]==0){
validNum--;
}
if(v[0]==0 && v[1]==0 && v[2]==0){
validNum--;
}
if(v[0]==0 && v[1]==0 && v[2]==0 && v[3]==0){
validNum--;
}
sort(v.begin(),v.end());
return pair<int,int>(calcMax(v,validNum),
v[0]*1000+v[1]*100+v[2]*10+v[3]);
}
int process(pair<int,int> p){
int count=1;
int diff=p.first-p.second;
cout<<p.first<<"-"<<p.second<<"="<<diff<<endl;
if( diff==6174 || diff==0){
return count;
}else{
//count++;
return count+=process(maxMin(diff));
}
}
int main(int argc, char *argv[]){
int num;
while(cin>>num,num!=-1){
cout<<"N="<<num<<":"<<endl;
pair<int,int> p=maxMin(num);
if(num<1000||num>9999||p.first==p.second){
cout<<"No!!"<<endl;
}else{
cout<<"Ok!! "<<process(p)<<" times"<<endl;
}
}
return 0;
}
POJ-1350
最新推荐文章于 2019-09-12 10:20:26 发布