PAT (Advanced Level) 1069. The Black Hole of Numbers (20)

时间:2021-11-02 15:36:17

简单题。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<algorithm>
using namespace std; int n;
int a[];
int num1,num2; int f(int num)
{
memset(a,,sizeof a);
int tmp=num;
int z=;
while(tmp) a[z++]=tmp%,tmp=tmp/;
num1=,num2=;
sort(a,a+);
for(int i=;i<;i++) num1=num1*+a[i];
for(int i=;i>=;i--) num2=num2*+a[i];
return num2-num1;
} int main()
{
scanf("%d",&n);
if(f(n)==) printf("%04d - %04d = %04d\n",n,n,);
else
{
while()
{
int res=f(n);
printf("%04d - %04d = %04d\n",num2,num1,res);
n=res;
if(n==) break;
}
}
return ;
}