大小之差

来源:互联网 发布:英雄联盟控软件 编辑:程序博客网 时间:2024/06/05 18:22
 某6位数,只包含1~9中的某些数字(可以重复包含同一数字,但不含数字0)。
    该数字重新排列数位后,可以得到最大数和最小数。最大最小之差也是6位数,并且它恰好包含了组成原6位数同样的数字。

    比如:766431 - 134667 = 631764 就是具有这样特征的数字。
    
    你还能找到另一个这样的6位数吗?
    
    请填写它重新排列数位后的得到的最大数:________________  


请通过浏览器提交答案。
注意:只提交另一个6位数,题中已经给出的这个不要提交。
注意:不要书写其它的内容(比如:说明性的文字)。

 1 import java.util.ArrayList;   2 /**  3  * 大小之差,字符串操作加上数组排序。  4  * @author YangCheney  5  * @Date 2016年12月1日  6  */   7 public class Test{   8        9     static int[] a=new int[6];  10     static int[] b=new int[6];  11       12     static ArrayList<String> al=new ArrayList<String>();  13     public static String BubSort(int a[]) {  14         boolean flag = true;  15         for (int i = 0; i < a.length && flag; i++) {  16             flag = false;  17             for (int j = a.length - 1; j > i; j--) {  18                 if (a[j] < a[j - 1]) {  19                     swap(a, j, j - 1);  20                     flag = true;  21                 }  22             }  23         }  24         String s="";  25         for(int i=0;i<6;i++){  26             s+=a[i];  27         }  28         return s;  29     }  30       31     public static void swap(int[] arr,int i,int j){  32         int temp=arr[i];  33         arr[i]=arr[j];  34         arr[j]=temp;  35     }  36       37     public static void function(int n){  38         String t1,t2,t3,t4;  39         String s=""+n;  40         if(s.indexOf("0") != -1)  41             return;  42         for(int j=0;j<6;j++){  43             a[j]=s.charAt(j)-48;  44         }  45         t1=BubSort(a);  46         StringBuffer sb=new StringBuffer(t1);  47         t2=sb.reverse().toString();  48         t3=(Integer.parseInt(t2)-Integer.parseInt(t1))+"";  49         if(t3.length()!=6)  50             return;  51         for(int j=0;j<6;j++){  52             b[j]=t3.charAt(j)-48;  53         }  54         t4=BubSort(b);  55           56         if(t1.equals(t4)){  57             if(!al.contains(t3)){  58                 al.add(t3);  59                 System.out.println(t2+"-"+t1+"="+t3);  60             }else {  61                 return;  62             }  63         }  64     }  65     public static void main(String[] args) {   66         for(int i=111111;i<=999999;i++){  67             function(i);  68         }  69     }  70 }  

 

0 0
原创粉丝点击