• 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 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 (.

    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

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1010;
    #define  inf  0x3fffffff
    int number[4];
    bool cmp(int a,int b){
        return a>b;
    }
    void tran(int n){
        int i=0;
        while(n!=0){
            number[i]=n%10;
            n/=10;
            i++;
        }
    }
    void init(){
        for(int i=0;i<=4;i++){
            number[i]=0;
        }
    }
    int main(){
        int m;
        scanf("%d",&m);
        if(m==0){
            printf("0000 - 0000 = 0000
    ");
            return 0;
        }
        if(m==6174){
            m=7641;
        }
        int n=m;
        tran(n);
        while(true){
            if(n==6174||n==0){
                break;
            }
            init();
            tran(n);
            sort(number,number+4,cmp);
            int num1=0;
            for(int i=0;i<4;i++){
                num1=num1*10+number[i];
            }
            sort(number,number+4);
            int num2=0;
            for(int i=0;i<4;i++){
                num2=num2*10+number[i];
            }
            n=num1-num2;
            printf("%04d - %04d = %04d
    ",num1,num2,n);
        }
        
        
        return 0;
    }
    //注意:0,1,11,111,1000,6174


  • 相关阅读:
    Servlet获取URL地址
    js实现浏览器通知功能
    利用Hibernate监听器实现用户操作日志
    XMLHttpRequest上传文件实现进度条
    事务配置中的一些要点
    Spring事务配置的五种方式
    基于注解的Spring AOP的配置和使用
    @ResponseBody注解与JSON
    springMVC获取request和response
    Highcharts属性介绍
  • 原文地址:https://www.cnblogs.com/dreamzj/p/14444612.html
Copyright © 2020-2023  润新知