zoukankan      html  css  js  c++  java
  • 数字黑洞 (20)

    给定任一个各位数字不完全相同的4位正整数,如果我们先把4个数字按非递增排序,再按非递减排序,然后用第1个数字减第2个数字,将得到
    一个新的数字。一直重复这样做,我们很快会停在有“数字黑洞”之称的6174,这个神奇的数字也叫Kaprekar常数。

    例如,我们从6767开始,将得到

    7766 - 6677 = 1089
    9810 - 0189 = 9621
    9621 - 1269 = 8352
    8532 - 2358 = 6174
    7641 - 1467 = 6174
    ... ...

    现给定任意4位正整数,请编写程序演示到达黑洞的过程。 
    输入描述:
    输入给出一个(0, 10000)区间内的正整数N。


    输出描述:
    如果N的4位数字全相等,则在一行内输出“N - N = 0000”;否则将计算的每一步在一行内输出,直到6174作为差出现,输出格式见样例。注意每个数字按4位数格

    式输出。

    输入例子:
    6767

    输出例子:
    7766 - 6677 = 1089

    9810 - 0189 = 9621

    9621 - 1269 = 8352

    8532 - 2358 = 6174

    import java.util.*;
    import java.text.DecimalFormat;;
    public class Main{
        public static void main(String[] args){
            Scanner in = new Scanner(System.in);
            int A;
            DecimalFormat format = new DecimalFormat("0000");
            while(in.hasNext()){
                A = in.nextInt();
                while(true){
                    if(A == 6174){
                        break;
                    }
                    if(A==1111 || A==2222 ||A==3333 ||A==4444 ||A==5555 ||A==6666|| 
                            A==7777 ||A==8888 ||A==999 ){
                        System.out.println(A +" - "+A+" = 0000");
                        break;
                    }
                    int[] res = change(A);
                    
                    int x = res[1] - res[0];
                    System.out.println(format.format(res[1]) + " - " +format.format(res[0]) +" = " +format.format(x) );
                    
                    A = x;
                    if(x<=0)
                        break;
                    
                    
                }
            }
        }
        public static int[] change(int A){
            int[] nums = new int[10];
            int[] res = new int[2];
            while(A>=1){
                nums[A%10]++;
                A=A/10;
                
            }
            int sum = 0;
            for(int a:nums)
                sum+=a;
            int B = 0;
            int C = 0;
            for(int i=0;i<10;i++){
                int count1 = nums[i];
                while(count1!=0){
                    B = B*10 + i;
                    count1--;
                }
                int count2 = nums[10-i-1];
                while(count2!=0){
                    C = C*10 + 10-i-1;
                    count2--;
                }
            }
            while(C<1000){
                C = C * 10;
            }
            res[0] = B;
            res[1] = C;
            return res;
        }
    }
     
  • 相关阅读:
    《Linux命令行与shell脚本编程大全 第3版》高级Shell脚本编程---07
    《Linux命令行与shell脚本编程大全 第3版》高级Shell脚本编程---05
    shell-查看手机分辨率
    imageView-scaleType 图片压缩属性
    actionbar-displayOptions 属性分析
    setting.system-全局属性的设定
    ActivityChooserView-如何隐藏选择的应用图标
    mIsFunui-判断Funui方法
    setting-在设置中添加新的选项
    install-软件安装跟push的区别
  • 原文地址:https://www.cnblogs.com/bbbblog/p/5724218.html
Copyright © 2011-2022 走看看