zoukankan      html  css  js  c++  java
  • 大小之差

     某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 }  
  • 相关阅读:
    (译)构建Async同步基元,Part 3 AsyncCountdownEvent
    (译)构建Async同步基元,Part 5 AsyncSemaphore
    SICP学习笔记(P3P17)
    关于汇编语言寄存器和指令操作的整理
    VS2010和IE8是怎样让"Ctrl+鼠标滚轮的上下操作"实现改变字体或页面大小的
    "六度空间"的应用——找出两个陌生人之间的关系(二)
    关于QQ一些功能的实现(二)
    用Socket做一个局域网聊天工具
    SICP学习笔记(P27P28)
    算法练习 (二)
  • 原文地址:https://www.cnblogs.com/YangGC/p/6188937.html
Copyright © 2011-2022 走看看