1069. The Black Hole of Numbers

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.

这题跟B19是一样的

#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
int Sort1(int a1,int a2,int a3,int a4);
int Sort2(int a1,int a2,int a3,int a4);
int main ()
{
    int a1=0,a2=0,a3=0,a4=0,i=0;
    int num;
    scanf("%d",&num);
    a1=num/1000;
        a2=(num/100)%10;
        a3=(num%100)/10;
        a4=num%10;

    if(a1==a2 && a2==a3 && a3==a4)
    {
        printf("%d%d%d%d - %d%d%d%d = 0000\n",a1,a1,a1,a1,a1,a1,a1,a1);
        return 0;
        }
    int result=0,first=0,second=0;
    while(1)
    {
        first=Sort2(a1,a2,a3,a4);
        second=Sort1(a1,a2,a3,a4);
        result=first-second;
        a1=result/1000;
        a2=(result/100)%10;
        a3=(result%100)/10;
        a4=result%10;
        printf("%04d - %04d = %04d\n",first,second,result);
        if(result==6174||result==0) break;
        }

    system("pause");
    return 0;
    }

int Sort1(int a1,int a2,int a3,int a4)
{
     int a[4]={a1,a2,a3,a4};
     sort(a,a+4);
     return (a[0]*1000+a[1]*100+a[2]*10+a[3]);
     }
bool cmp(int a,int b)
{
     return a>b;
     }
int Sort2(int a1,int a2,int a3,int a4)
{
     int a[4]={a1,a2,a3,a4};
     sort(a,a+4,cmp);
     return (a[0]*1000+a[1]*100+a[2]*10+a[3]);
     }
时间: 2024-10-12 23:52:40

1069. The Black Hole of Numbers的相关文章

PAT 1069. The Black Hole of Numbers (20)

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

1069. The Black Hole of Numbers (20)【模拟】——PAT (Advanced Level) Practise

题目信息 1069. The Black Hole of Numbers (20) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B 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 n

PAT 甲级 1069 The Black Hole of Numbers (20 分)(内含别人string处理的精简代码)

1069 The Black Hole of Numbers (20 分) 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

1069. The Black Hole of Numbers (20)

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

PAT Advanced 1069 The Black Hole of Numbers (20分)

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

PAT:1069. The Black Hole of Numbers (20) AC

#include<stdio.h> #include<algorithm> using namespace std; const int AIM=6174; int n; int arr[4]; bool NonIncreasingOrder(int a,int b) { return a>b; } bool NonDecreasingOrder(int a,int b) { return a<b; } int toNum() //得到这个数字 { int sum=0;

PAT 1069. The Black Hole of Numbers

我觉得做人最重要的是把事情讲清楚,尤其是规则,如果法律条例不清楚,就可以寻租了 #include <cstdio> #include <cstdlib> #include <vector> #include <algorithm> using namespace std; int ensure(int n) { if (n < 10) { n *= 1000; } else if (n < 100) { n *= 100; } else if (

pat1069. The Black Hole of Numbers (20)

1069. 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-

The Black Hole of Numbers (strtoint+inttostr+sort)

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