PAT1069. The Black Hole of Numbers

//这是到水题,之前因为四位数的原因一直不能A,看了别人的程序,才明白,不够四位的时候没考虑到,坑啊。。。。。脸打肿

 

#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
//freopen("input.txt","r",stdin);
int i,n,s[5];
while(scanf("%d",&n)!=EOF)
{
while(1)
{
int len=0;
fill(s,s+4,0);
for(i=0;i<4;i++)
{
s[3-i]=n%10;
n/=10;
}
sort(s,s+4);
int n1=0,n2=0;
for(i=0;i<4;i++)
{
n1=n1*10+s[i];
n2=n2*10+s[4-1-i];
}
n=n2-n1;
printf("%04d - %04d = %04d\n",n2,n1,n);
if(n==0||n==6174)break;
}
}
return 0;
}

你可能感兴趣的:(PAT1069. The Black Hole of Numbers)