[Grade] PAT 1069 The Black Hole of Numbers (20 points)

Meaning of the questions:

After four digits of four digits input a four-digit positive integer N, the output of each digit in descending order after subtracting the ascending order of four digits equal, or if the result is all the same number 6174 (black holes cycle number) is stopped .

trick:

This question is uncharacteristically digital input instead of an int type always use leading zero string of the input, so the test points 2,3,4 timeout if the input string. . . . .

Code:

#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
int n;
char x[7],y[7];
int main(){
scanf("%d",&n);
x[0]=n/1000+'0';
n%=1000;
x[1]=n/100+'0';
n%=100;
x[2]=n/10+'0';
n%=10;
x[3]=n+'0';
while(1){
if(x[0]==x[1]&&x[1]==x[2]&&x[2]==x[3]){
printf("%s - %s = 0000",x,x);
//cout<<x<<" - "<<x<<" = 0000";
return 0;
}
sort(x,x+4);
y[0]=x[3];
y[1]=x[2];
y[2]=x[1];
y[3]=x[0];
int xx=(x[0]-'0')*1000+(x[1]-'0')*100+(x[2]-'0')*10+x[3]-'0';
int yy=(y[0]-'0')*1000+(y[1]-'0')*100+(y[2]-'0')*10+y[3]-'0';
int zz=yy-xx;
printf("%04d - %04d = %04d",yy,xx,zz);
//cout<<yy<<" - "<<xx<<" = "<<zz;
if(zz==6174)
break;
else
printf("\n");
//cout<<"\n";
x[0]=zz/1000+'0';
zz%=1000;
x[1]=zz/100+'0';
zz%=100;
x[2]=zz/10+'0';
zz%=10;
x[3]=zz+'0';
}
return 0;
}

Guess you like

Origin www.cnblogs.com/ldudxy/p/11785050.html