http://bbs.51js.com/viewthread.php?tid=45812&highlight=%2B155120699

解决方案 »

  1.   

    思路:五位数编号0-4,我们只考虑第i+1位不比第i位大即可,这样省去了验重的麻烦~
    代码如下:public static void zuhe(){
      long t1 = System.currentTimeMillis();
      int _i = 0,_j = 0,_k = 0,_m = 0;
      int count = 0;
      for (int i = 15; (_i = i) >= 1;i --){
        for (int j = _i; (_j = j) >= 1; j --){
          for (int k = _j; (_k = k) >= 1; k --){
            for (int m = _k; (_m = m) >= 1; m --){
              for (int n = _m; n >= 1; n --){
                System.out.println(Integer.toString(i, 16) + "," + 
           Integer.toString(j, 16) + "," + 
           Integer.toString(k, 16) + "," +
           Integer.toString(m, 16) + "," +
           Integer.toString(n, 16));
                count ++;
              }
            }
          }
        }
      }
      System.out.println("符合条件总个数:" + count);
      long t2 = System.currentTimeMillis();
      System.out.println("TotalTime = " + (t2-t1) + "毫秒.");
    }
    测试结果:
    ........
    4,4,4,1,1
    4,4,3,3,3
    4,4,3,3,2
    4,4,3,3,1
    4,4,3,2,2
    4,4,3,2,1
    4,4,3,1,1
    4,4,2,2,2
    4,4,2,2,1
    4,4,2,1,1
    4,4,1,1,1
    4,3,3,3,3
    4,3,3,3,2
    4,3,3,3,1
    4,3,3,2,2
    4,3,3,2,1
    4,3,3,1,1
    4,3,2,2,2
    4,3,2,2,1
    4,3,2,1,1
    4,3,1,1,1
    4,2,2,2,2
    4,2,2,2,1
    4,2,2,1,1
    4,2,1,1,1
    4,1,1,1,1
    3,3,3,3,3
    3,3,3,3,2
    3,3,3,3,1
    3,3,3,2,2
    3,3,3,2,1
    3,3,3,1,1
    3,3,2,2,2
    3,3,2,2,1
    3,3,2,1,1
    3,3,1,1,1
    3,2,2,2,2
    3,2,2,2,1
    3,2,2,1,1
    3,2,1,1,1
    3,1,1,1,1
    2,2,2,2,2
    2,2,2,2,1
    2,2,2,1,1
    2,2,1,1,1
    2,1,1,1,1
    1,1,1,1,1符合条件总个数:11628
    TotalTime = 203毫秒.
      

  2.   

    晕,三楼写滴是啥呀?!lz要滴是c(15,5)组合结果,合计也不过才是4368!
      

  3.   

    JavaScript改一点就可以了:function zuhe(){
      var t1 = new Date();
      var _i = 0,_j = 0,_k = 0,_m = 0;
      var count = 0;
      for (var i = 15; (_i = i) >= 1;i --){
        for (var j = _i; (_j = j) >= 1; j --){
          for (var k = _j; (_k = k) >= 1; k --){
            for (var m = _k; (_m = m) >= 1; m --){
              for (var n = _m; n >= 1; n --){
                document.write(i.toString(16) + "," + j.toString(16) + "," + k.toString(16) + "," + 
                               m.toString(16) + "," + n.toString(16) + "<br>");
                count ++;
              }
            }
          }
        }
      }
      document.write("符合条件总个数:" + count + "<br>");
      var t2 = new Date();
      document.write("TotalTime = " + (t2-t1) + "毫秒.<br>");
    }
      

  4.   

    不好意思,我写的是允许不同位数字重复的情况,如果不允许数字相同的话去掉前面的等号就可以
    public static void zuhe(){
      long t1 = System.currentTimeMillis();
      int _i = 0,_j = 0,_k = 0,_m = 0;
      int count = 0;
      for (int i = 15; (_i = i) >= 1;i --){
        for (int j = _i-1; (_j = j) >= 1; j --){
          for (int k = _j-1; (_k = k) >= 1; k --){
            for (int m = _k-1; (_m = m) >= 1; m --){
              for (int n = _m-1; n >= 1; n --){
                System.out.println(Integer.toString(i, 16) + "," + 
                       Integer.toString(j, 16) + "," + 
                       Integer.toString(k, 16) + "," +
                       Integer.toString(m, 16) + "," +
                       Integer.toString(n, 16));
                count ++;
              }
            }
          }    
        }
      }
      System.out.println("符合条件总个数:" + count);
      long t2 = System.currentTimeMillis();        
      System.out.println("TotalTime = " + (t2-t1) + "毫秒.");
    }
    结果:
    ........
    7,6,4,3,1
    7,6,4,2,1
    7,6,3,2,1
    7,5,4,3,2
    7,5,4,3,1
    7,5,4,2,1
    7,5,3,2,1
    7,4,3,2,1
    6,5,4,3,2
    6,5,4,3,1
    6,5,4,2,1
    6,5,3,2,1
    6,4,3,2,1
    5,4,3,2,1
    符合条件总个数:3003
    TotalTime = 93毫秒.4楼好像说错了~
    C(15,5) = (15*14*13*12*11)/(1*2*3*4*5)=3003,不是4368~~~所以上面11628是允许数字重复的个数,3003是不允许重复的个数(好像lz没说不允许重复吧)
     
      

  5.   

    今天看到一个高手的写法,感觉很不错,拿来分享~public class MyTest {
      public static void main(String[] args) {
        String array[] = {"1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};
        long t1 = System.currentTimeMillis();
        Object[] arr = subArraysByLength(array,5);
        System.out.println(Arrays.deepToString(arr));
        System.out.println("一共有" + arr.length + "个元素.");
        long t2 = System.currentTimeMillis();
        System.out.println("一共花费时间" + (t2-t1) + "毫秒.");
      }
      /**
       * 取得集合array的所有长度为eleNum的子集合
       * 若eleNum<0则取得所有子集合
       * @param array
       * @param length
       * @return
       */
      public static Object[] subArraysByLength(Object[] array,int eleNum){
        if (array == null){
          return null;
        }
        int length = array.length;
        List<Object[]> list = new ArrayList<Object[]>();
        for (int i = 0; i < (1 << length); i ++){
          if (!(eleNum < 0 || get1Cnt(i) == eleNum)){
            continue;
          }
          List<Object> _list = new ArrayList<Object>();
          for (int j = 0; j < length; j ++){
            if((i&(1<<j)) != 0){
              _list.add(array[j]);
            }
          }
          list.add(_list.toArray(new Object[_list.size()]));
        }
        return list.toArray(new Object[list.size()]);
      }
      /**
       * 取得二进制正整数中"1"的个数
       * @param num
       * @return
       */
      private static int get1Cnt(int num){
        int cnt = 0;
        for (;num > 0;num >>= 1){
          cnt += (num&1);
        }
        return cnt;
      }
    }
    测试结果:
    [...,[5, a, c, d, f], [6, a, c, d, f], [7, a, c, d, f], [8, a, c, d, f], [9, a, c, d, f], [1, b, c, d, f], [2, b, c, d, f], [3, b, c, d, f], [4, b, c, d, f], [5, b, c, d, f], [6, b, c, d, f], [7, b, c, d, f], [8, b, c, d, f], [9, b, c, d, f], [a, b, c, d, f], [1, 2, 3, e, f], [1, 2, 4, e, f], [1, 3, 4, e, f], [2, 3, 4, e, f], [1, 2, 5, e, f], [1, 3, 5, e, f], [2, 3, 5, e, f], [1, 4, 5, e, f], [2, 4, 5, e, f], [3, 4, 5, e, f], [1, 2, 6, e, f], [1, 3, 6, e, f], [2, 3, 6, e, f], [1, 4, 6, e, f], [2, 4, 6, e, f], [3, 4, 6, e, f], [1, 5, 6, e, f], [2, 5, 6, e, f], [3, 5, 6, e, f], [4, 5, 6, e, f], [1, 2, 7, e, f], [1, 3, 7, e, f], [2, 3, 7, e, f], [1, 4, 7, e, f], [2, 4, 7, e, f], [3, 4, 7, e, f], [1, 5, 7, e, f], [2, 5, 7, e, f], [3, 5, 7, e, f], [4, 5, 7, e, f], [1, 6, 7, e, f], [2, 6, 7, e, f], [3, 6, 7, e, f], [4, 6, 7, e, f], [5, 6, 7, e, f], [1, 2, 8, e, f], [1, 3, 8, e, f], [2, 3, 8, e, f], [1, 4, 8, e, f], [2, 4, 8, e, f], [3, 4, 8, e, f], [1, 5, 8, e, f], [2, 5, 8, e, f], [3, 5, 8, e, f], [4, 5, 8, e, f], [1, 6, 8, e, f], [2, 6, 8, e, f], [3, 6, 8, e, f], [4, 6, 8, e, f], [5, 6, 8, e, f], [1, 7, 8, e, f], [2, 7, 8, e, f], [3, 7, 8, e, f], [4, 7, 8, e, f], [5, 7, 8, e, f], [6, 7, 8, e, f], [1, 2, 9, e, f], [1, 3, 9, e, f], [2, 3, 9, e, f], [1, 4, 9, e, f], [2, 4, 9, e, f], [3, 4, 9, e, f], [1, 5, 9, e, f], [2, 5, 9, e, f], [3, 5, 9, e, f], [4, 5, 9, e, f], [1, 6, 9, e, f], [2, 6, 9, e, f], [3, 6, 9, e, f], [4, 6, 9, e, f], [5, 6, 9, e, f], [1, 7, 9, e, f], [2, 7, 9, e, f], [3, 7, 9, e, f], [4, 7, 9, e, f], [5, 7, 9, e, f], [6, 7, 9, e, f], [1, 8, 9, e, f], [2, 8, 9, e, f], [3, 8, 9, e, f], [4, 8, 9, e, f], [5, 8, 9, e, f], [6, 8, 9, e, f], [7, 8, 9, e, f], [1, 2, a, e, f], [1, 3, a, e, f], [2, 3, a, e, f], [1, 4, a, e, f], [2, 4, a, e, f], [3, 4, a, e, f], [1, 5, a, e, f], [2, 5, a, e, f], [3, 5, a, e, f], [4, 5, a, e, f], [1, 6, a, e, f], [2, 6, a, e, f], [3, 6, a, e, f], [4, 6, a, e, f], [5, 6, a, e, f], [1, 7, a, e, f], [2, 7, a, e, f], [3, 7, a, e, f], [4, 7, a, e, f], [5, 7, a, e, f], [6, 7, a, e, f], [1, 8, a, e, f], [2, 8, a, e, f], [3, 8, a, e, f], [4, 8, a, e, f], [5, 8, a, e, f], [6, 8, a, e, f], [7, 8, a, e, f], [1, 9, a, e, f], [2, 9, a, e, f], [3, 9, a, e, f], [4, 9, a, e, f], [5, 9, a, e, f], [6, 9, a, e, f], [7, 9, a, e, f], [8, 9, a, e, f], [1, 2, b, e, f], [1, 3, b, e, f], [2, 3, b, e, f], [1, 4, b, e, f], [2, 4, b, e, f], [3, 4, b, e, f], [1, 5, b, e, f], [2, 5, b, e, f], [3, 5, b, e, f], [4, 5, b, e, f], [1, 6, b, e, f], [2, 6, b, e, f], [3, 6, b, e, f], [4, 6, b, e, f], [5, 6, b, e, f], [1, 7, b, e, f], [2, 7, b, e, f], [3, 7, b, e, f], [4, 7, b, e, f], [5, 7, b, e, f], [6, 7, b, e, f], [1, 8, b, e, f], [2, 8, b, e, f], [3, 8, b, e, f], [4, 8, b, e, f], [5, 8, b, e, f], [6, 8, b, e, f], [7, 8, b, e, f], [1, 9, b, e, f], [2, 9, b, e, f], [3, 9, b, e, f], [4, 9, b, e, f], [5, 9, b, e, f], [6, 9, b, e, f], [7, 9, b, e, f], [8, 9, b, e, f], [1, a, b, e, f], [2, a, b, e, f], [3, a, b, e, f], [4, a, b, e, f], [5, a, b, e, f], [6, a, b, e, f], [7, a, b, e, f], [8, a, b, e, f], [9, a, b, e, f], [1, 2, c, e, f], [1, 3, c, e, f], [2, 3, c, e, f], [1, 4, c, e, f], [2, 4, c, e, f], [3, 4, c, e, f], [1, 5, c, e, f], [2, 5, c, e, f], [3, 5, c, e, f], [4, 5, c, e, f], [1, 6, c, e, f], [2, 6, c, e, f], [3, 6, c, e, f], [4, 6, c, e, f], [5, 6, c, e, f], [1, 7, c, e, f], [2, 7, c, e, f], [3, 7, c, e, f], [4, 7, c, e, f], [5, 7, c, e, f], [6, 7, c, e, f], [1, 8, c, e, f], [2, 8, c, e, f], [3, 8, c, e, f], [4, 8, c, e, f], [5, 8, c, e, f], [6, 8, c, e, f], [7, 8, c, e, f], [1, 9, c, e, f], [2, 9, c, e, f], [3, 9, c, e, f], [4, 9, c, e, f], [5, 9, c, e, f], [6, 9, c, e, f], [7, 9, c, e, f], [8, 9, c, e, f], [1, a, c, e, f], [2, a, c, e, f], [3, a, c, e, f], [4, a, c, e, f], [5, a, c, e, f], [6, a, c, e, f], [7, a, c, e, f], [8, a, c, e, f], [9, a, c, e, f], [1, b, c, e, f], [2, b, c, e, f], [3, b, c, e, f], [4, b, c, e, f], [5, b, c, e, f], [6, b, c, e, f], [7, b, c, e, f], [8, b, c, e, f], [9, b, c, e, f], [a, b, c, e, f], [1, 2, d, e, f], [1, 3, d, e, f], [2, 3, d, e, f], [1, 4, d, e, f], [2, 4, d, e, f], [3, 4, d, e, f], [1, 5, d, e, f], [2, 5, d, e, f], [3, 5, d, e, f], [4, 5, d, e, f], [1, 6, d, e, f], [2, 6, d, e, f], [3, 6, d, e, f], [4, 6, d, e, f], [5, 6, d, e, f], [1, 7, d, e, f], [2, 7, d, e, f], [3, 7, d, e, f], [4, 7, d, e, f], [5, 7, d, e, f], [6, 7, d, e, f], [1, 8, d, e, f], [2, 8, d, e, f], [3, 8, d, e, f], [4, 8, d, e, f], [5, 8, d, e, f], [6, 8, d, e, f], [7, 8, d, e, f], [1, 9, d, e, f], [2, 9, d, e, f], [3, 9, d, e, f], [4, 9, d, e, f], [5, 9, d, e, f], [6, 9, d, e, f], [7, 9, d, e, f], [8, 9, d, e, f], [1, a, d, e, f], [2, a, d, e, f], [3, a, d, e, f], [4, a, d, e, f], [5, a, d, e, f], [6, a, d, e, f], [7, a, d, e, f], [8, a, d, e, f], [9, a, d, e, f], [1, b, d, e, f], [2, b, d, e, f], [3, b, d, e, f], [4, b, d, e, f], [5, b, d, e, f], [6, b, d, e, f], [7, b, d, e, f], [8, b, d, e, f], [9, b, d, e, f], [a, b, d, e, f], [1, c, d, e, f], [2, c, d, e, f], [3, c, d, e, f], [4, c, d, e, f], [5, c, d, e, f], [6, c, d, e, f], [7, c, d, e, f], [8, c, d, e, f], [9, c, d, e, f], [a, c, d, e, f], [b, c, d, e, f]]一共有3003个元素.
    一共花费时间32毫秒.
      

  6.   

    注意:上面算法eleNum别取<0不然电脑不好的话会死的很难看的~
    (集合元素个数少点就可以了!!)
    String array[] = {"1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};
    long t1 = System.currentTimeMillis();
    Object[] arr = subArraysByLength(array,-2);
    System.out.println(Arrays.deepToString(arr));
    System.out.println("一共有" + arr.length + "个元素.");
    long t2 = System.currentTimeMillis();
    System.out.println("一共花费时间" + (t2-t1) + "毫秒.");
    我执行了一下,2G内存电脑等了半天才有反应~
    最后结果:一共有32768个元素.
    一共花费时间30500毫秒.