B1069 The Black Hole of Numbers
2020-02-14 本文已影响0人
Tsukinousag
B1069 The Black Hole of Numbers (20分)
//注意数字处理时不足四位补上前导0.
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
#define lowbit(i)((i)&(-i))
using namespace std;
typedef long long ll;
const int MAX=3e5+10;
const int INF=0x3f3f3f3f;
const int MOD=1000000007;
const int SQR=632;//633块,632
bool cmp(char a,char b)
{
return a>b;
}
bool cmp2(char a,char b)
{
return a<b;
}
string change(int n)
{
string ans="";
while(n>0)
{
char c=n%10+'0';
ans+=c;
n/=10;
}
while(ans.size()<4)
ans='0'+ans;
return ans;
}
int change2(string x)
{
int sum=0;
for(int i=0;i<x.size();i++)
{
sum=sum*10+x[i]-'0';
}
return sum;
}
int main()
{
int n;
string a,b,temp;
scanf("%d",&n);
int t=n;
while(1)
{
temp=change(t);
sort(temp.begin(),temp.end(),cmp);
a=temp;
printf("%04d",change2(a));
printf(" - ");
sort(temp.begin(),temp.end(),cmp2);
b=temp;
printf("%04d",change2(b));
printf(" = ");
t=change2(a)-change2(b);
printf("%04d\n",t);
if(t==6174||t==0)
break;
}
return 0;
}