- The Black Hole of Numbers (20)
时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in non-increasing order first, and then in non-decreasing order, a new number can be obtained by taking the second number from the first one. Repeat in this manner we will soon end up at the number 6174 – the “black hole” of 4-digit numbers. This number is named Kaprekar Constant.
For example, start from 6767, we’ll get:
7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
7641 - 1467 = 6174
… …
Given any 4-digit number, you are supposed to illustrate the way it gets into the black hole.
Input Specification:
Each input file contains one test case which gives a positive integer N in the range (0, 10000).
Output Specification:
If all the 4 digits of N are the same, print in one line the equation “N - N = 0000”. Else print each step of calculation in a line until 6174 comes out as the difference. All the numbers must be printed as 4-digit numbers.
Sample Input 1:
6767
Sample Output 1:
7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
Sample Input 2:
2222
Sample Output 2:
2222 - 2222 = 0000
只要建立数字和数组的关系即可。注意开始要输入到数字里,不然若输入不满4位数,输入到数组可能会出错。
#include<iostream>
#include<algorithm>
using namespace std;
int getnum(char num[]){
int t=0;
for(int i=0;i<=3;i++){
t=t*10+(num[i]-'0');
}
return t;
}
void numtstr(int n,char *str){
for(int i=3;i>=0;i--){
str[i]=n%10+'0';
n/=10;
}
}
int reverse(int n){
int t=0;
for(int i=0;i<4;i++){
t=t*10+n%10;
n/=10;
}
return t;
}
bool cmp(char a,char b){
return a>b;
}
int main(){
int n,n1,t;
char num[4]={0};
scanf("%d",&t);
numtstr(t,num);
sort(num,num+4,cmp);
n=getnum(num);
while(1){
n1=reverse(n);
t=n-n1;
printf("%04d - %04d = %04d\n",n,n1,t);
if(t==0||t==6174) break;
numtstr(t,num);
sort(num,num+4,cmp);
n=getnum(num);
}
return 0;
}
(以前写的)用节点写的,实际上不需要建立结点,直接基于数组进行运算就好。
#include<iostream>
#include<algorithm>
using namespace std;
bool compare(int a,int b)
{
return a>b;
}
struct node
{
public:
int a[4];
node(){}
int m;
node(int n)
{
a[0]=n/1000;
a[1]=(n/100)%10;
a[2]=(n/10)%10;
a[3]=(n%10);
sort(a,a+4,compare);
}
node(int b[])
{
a[0]=b[3];
a[1]=b[2];
a[2]=b[1];
a[3]=b[0];
}
node(int de[],int in[])
{
/*
a[0]=de[0]-in[0];
a[1]=de[1]-in[1];
a[2]=de[2]-in[2];
a[3]=de[3]-in[3];
*/
int s1=de[0]*1000+de[1]*100+de[2]*10+de[3];
int s2=in[0]*1000+in[1]*100+in[2]*10+in[3];
int n=s1-s2;
a[0]=n/1000;
a[1]=(n/100)%10;
a[2]=(n/10)%10;
a[3]=(n%10);
m=n;
sort(a,a+4,compare);
}
void pirntm()
{
printf("%d%d%d%d", m/1000,(m/100)%10
,(m/10)%10
,(m%10));
}
void printnum()
{
printf("%d%d%d%d",a[0],a[1],a[2],a[3]);
}
int check()
{
int s=a[0]*1000+a[1]*100+a[2]*10+a[3];
if(m==0)
return 0;
else if(m==6174)
return 1;
else return -1;
}
};
int main()
{
int n;
cin>>n;
//n=1231;
node s1=node(n);
node s2=node(s1.a);
while(s1.check()==-1)
{
node s=node(s1.a,s2.a);
s1.printnum();cout<<" - ";s2.printnum();cout<<" = ";s.pirntm();cout<<endl;
s1=s;
s2=node(s1.a);
}
system("pause");
return 0;
}