题目要求:
1、从1~31个数字当中随机抽出8位数字之和加起等于一个(指定的数字
2、并把所有等于这个指定的数字的8位数字全部列出来。比如:
1、2、3、4、5、6、7、8、9、10、11、12、13、14、15、16、17、18、19、20、21、22、23、24、25、26、27、28、29、30、31(从这1~31个数字当中随机抽 8个数字)
1+5+9+11+13+19+22+27 = 107
1+5+9+11+15+19+22+25 = 107
..............       = 107
..............       = 107
          .
          ...............       = 107
就这样子全部列出来 ,请帮我解题:非常感谢...

解决方案 »

  1.   


    请用Java算法 帮我解决这道题目  谢谢!!!~
      

  2.   

    抵制问作业..CSDN是讨论不是帮学生们做作业的...最起码你自己编个代码出来..让别人来替你修改都可以...你这样太没有诚意了.
      

  3.   


    private static int[] array = {1,2,3,4,5,6,7,8,9,10,11,12};

    private final static int result = 35;
    public static void getCollection(int [] array,int result) { long bitMap = 1 << (array.length);
    for (long i = 1; i < bitMap; i++) {
    int temp = 0;
    String tempstr = "{";
    for (int j = 0; j < array.length; j++) {
    if ((i >> j & 1) != 0) {
    temp += array[j];
    tempstr += " " + array[j];
    }
    }
    if (temp == result) {
    System.out.println(tempstr + "}");
    }
    }给你个例子,自己看看吧
      

  4.   

    假设设定的数为M
    if(M<1+2+3+....+8||M>24+25+....+31)
    {
       System.out.println("无法满足你的要求!");
    }
    else
    {
      int index=M-(1+2+3+.....8);
      //以M为基准,划分给前面8个数,且划分后得到的新块大小在区间【1,31】之间即可
    }
      

  5.   

    不好意思,错啦
    是以index为基准
      

  6.   

    以前用js写过一个求一组序列中N个元素的子组合的问题,是用动态生成Function解决的,
    但对于Java这类编译语言一直没有找到好的解法,来模拟多重for循环。今天状态不错,
    很快解决了,自认为还算满意,放上来请大家拍砖package com.saturday;/**
     * 问题的要求:
     *    从1-31的整数序列中随机挑选8个元素,使其之和等于某一具体值
     * 
     * 分析问题  :
     *    1.问题可以拆分为两个子问题:如何从列出序列所有8元素的组合
     *    2.从所有组合中挑选出符合要求的组合并输出
     *    
     * 解决问题  :
     *    问题的难点在于如何获取8为组合,而8为组合可以看做一个按升序
     *    或降序排列的8位排列,所以也就容易了.首先从1-31中挑选一个
     *    元素作为第一个,然后从(第一个+1~31)中挑选一个作为第二个
     *    依次类推.
     * @author : 王建波
     * @date   : 2009-03-11
     * @version:1.0  beta
     */
    public class HistogramDemo2{
      
        public static void main(String[] args){
         int[] a=new int[]{31,1,2,3,4,5,6,7,8};
         int lSum=107;
        
         while(hasNext(a)){
         if(isMatch(a,lSum))
         outPut(a);
        
         next(a);
         }       
        }
        
        
        
        /**
         * 是否还有组合样本
         * @param a 现有组合样本
         * @return
         */
        public static boolean hasNext(int[] a){
         boolean hasNext=a[1]<=(a[0]-a.length+2);         
         return hasNext;
        }
        
        
        /**
         * 判断组合样本是否符合要求
         * @param a      组合样本数组
         * @param lSum   样本之和
         * @return
         */
        public static boolean isMatch(int[] a,long lSum){
         long _lSum=0;
        
         for(int i=a.length-1;i>=1;i--)
         _lSum+=a[i];
        
         return _lSum==lSum;
        }
        
        
        /**
         * 获取下一个样本
         * @param a
         */
        public static void next(int a[]){
         int i=a.length-1;
         while(i>=1){         
         if(a[i]<a[0]){
         a[i]=(a[i]<a[0])?(a[i]+1):(a[i-1]+1);
         break;
         }else{
         a[i]=(a[i]<a[0])?(a[i]+1):(a[i-1]+1);
         i--;
         }
         }    
        }
        
        
        /**
         * 输出组合样本
         * @param a
         */
        public static void outPut(int[] a){
         StringBuffer sb=new StringBuffer();
        
         for(int i=a.length-1;i>=1;i--)
         sb.append(a[i]+" ");
        
         System.out.println(sb.toString());
        }
    }
      

  7.   

    LS的算法我测试了,会出现重复的数字
    我是才学java,下边的方法比较笨拙,写出来大家指教,期待高手写出更简练的!!class AAA{
        public static void main(String[] args){
    int[] ar = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
    int sum = 107;

    for(int a = 0; a < ar.length-7; a++){
        for(int b = a+1; b < ar.length-6; b++){
    for(int c = b+1; c < ar.length-5; c++){
        for(int d = c+1; d < ar.length-4; d++){
    for(int e = d+1; e < ar.length-3; e++){
        for(int f = e+1; f < ar.length-2; f++){
    for(int g = f+1; g < ar.length-1; g++){
        for(int h = g+1; h < ar.length; h++){
    int total = ar[a]+ar[b]+ar[c]+ar[d]+ar[e]+ar[f]+ar[g]+ar[h];
    if(total == sum){
        System.out.println(ar[a]+"+"+ar[b]+"+"+ar[c]+"+"+ar[d]+"+"+ar[e]+"+"+ar[f]+"+"+ar[g]+"+"+ar[h]+" = 107");
    }
        }
    }
        }
    }
        }
    }
        }
    }
        }
    }
      

  8.   

    http://blog.csdn.net/luojihaidao/archive/2009/03/10/3976776.aspx