输入一个n 和 k 求 从n个字母中挑出k个字母的所有组合
没有编译错误,但是没有输出 为甚阿??
是不是arrList出错了?
代码如下:
import java.util.Scanner;
import java.util.ArrayList;public class test{
       public static void main(String[] args) {         Scanner input = new Scanner(System.in);
         System.out.print("Length");
         int n = input.nextInt();         int k = input.nextInt();
         char arrinput[] = new char [n] ;
         int num = 0;
         for (char i = 'A'; num <= n - 1 ; i++){  // 生成含n个字母的数组
         arrinput[num] = i;
         num++;
         }
      
         char[][] arr = combinations(arrinput, k);
         
         for (int i = 0; i < arr.length; i++) 
         {
             for (int j = 0; j < arr[i].length; j++)
             System.out.print(arr[i][j] + " ");
            System.out.print("  ");
         }
       }
    /**
     * @param charArray 输入的数组
     * @param k    选取的个数
     */
       
       public static char[][] combinations(char[] charArray, int k) {
         ArrayList<char[]> arrList = new ArrayList<char[]>();
         int record[] = new int[k];
         int n = charArray.length;
         combination(record, charArray, n, k, 0, 0);         char[][] combinationsArr = new char[arrList.size()][k];
         for (int i = 0; i < arrList.size(); i++)
         {combinationsArr[i] = arrList.get(i);}
         return combinationsArr;
       }
     /**
     * @param record      记录组合序列数组
     * @param charArray   记录组合序列数组
     * @param n           长度
     * @param k           选取的个数
     * @param a           数组坐标
     *                    (初始传入0)
     * @param b           辅助参数
     *                    (初始传入0)
     */
       public static void combination(int record[], char charArray[], int n, int k, int a, int b){
         ArrayList<char[]> arrList = new ArrayList<char[]>();  // 这里需要写么
         if (a == k) { 
                       char[] resultArr = new char[k];
                       for (int i = 0; i < k; ++i)
                       resultArr[i] = charArray[record[i] - 1];
                       arrList.add(resultArr);                       
                      } 
         else
              for (int i = b; i < n; i++) {
                                            record[a] = i + 1; 
                                            combination(record, charArray, n, k, a + 1, i + 1); 
                                            }
       
       }
}

解决方案 »

  1.   

    额额 如果我的代码比较难改 能不能写个输入一个n 和 k 求 从n个字母中挑出k个字母的所有组合 的代码阿高手。。返回一个二维数组
    public static char[][] combinations(char[] charArray, int k)  用这个method
      

  2.   

    import java.util.ArrayList;
    import java.util.Arrays;public class App{
        static int count = 0;  
        static char[] buf = {'1', '2', '3', '4','5'};    
        static ArrayList<String> list = new ArrayList<String>();
        
        public static void main(String[] args) {    
            select(buf, list, 3);  
            
            for(String str : list){  
                System.out.println(str);
            }   
        }    
        public static void select(char[] source, ArrayList<String> arrayList, int num){  
            int l = source.length;  
              
            char[] temp = new char[num];  
              
            System.arraycopy(source, 0, temp, 0, num);  //首先找到第一种组合
              
            arrayList.add(new String(temp));  //加到结果列表里
              
            for(int i=num; i<l; i++){  
                for (int j=0; j<num; j++){  
                    char tempChar = temp[j];  
                    temp[j] = source[i];  
                          
                    arrayList.add(new String(temp));  
                      
                    temp[j] = tempChar;  
                }  
            }  
        }     
    }
      

  3.   

    上面这个代码有问题:重新贴一个从别处转来的:package algorithm;
    //组合算法   
    //本程序的思路是开一个数组,其下标表示1到m个数,数组元素的值为1表示其下标   
    //代表的数被选中,为0则没选中。   
    //首先初始化,将数组前n个元素置1,表示第一个组合为前n个数。   
    //然后从左到右扫描数组元素值的"10"组合,找到第一个"10"组合后将其变为   
    //"01"组合,同时将其(在本算法中具体指i的位置)左边的所有"1"全部移动到数组的最左端。   
    //当第一个"1"移动到数组的m-n的位置,即n个"1"全部移动到最右端时,就得   
    //到了最后一个组合。   
    //例如求5中选3的组合:   
    //1 1 1 0 0 //1,2,3   
    //1 1 0 1 0 //1,2,4   
    //1 0 1 1 0 //1,3,4   
    //0 1 1 1 0 //2,3,4   
    //1 1 0 0 1 //1,2,5   
    //1 0 1 0 1 //1,3,5   
    //0 1 1 0 1 //2,3,5   
    //1 0 0 1 1 //1,4,5   
    //0 1 0 1 1 //2,4,5   
    //0 0 1 1 1 //3,4,5   
    import java.util.ArrayList;
    import java.util.List;
     
    /**  
     * java 代码实现组合的算法  
     * 从n个数里取出m个数的组合是n*(n-1)*...*(n-m+1)/m*(m-1)*...2*1   
     * @author dubensong  
     *  
     */
    public class AssemblyDemo {
        /**  
         * @param a:组合数组  
         * @param m:生成组合长度  
         * @return :所有可能的组合数组列表  
         */
        private List<int[]> combination(int[] a, int m) {
         AssemblyDemo c = new AssemblyDemo();
            List<int[]> list = new ArrayList<int[]>();
            int n = a.length;
            boolean end = false; // 是否是最后一种组合的标记   
            // 生成辅助数组。首先初始化,将数组前n个元素置1,表示第一个组合为前n个数。   
            int[] tempNum = new int[n];
            for (int i = 0; i < n; i++) {
                if (i < m) {
                    tempNum[i] = 1;
     
                } else {
                    tempNum[i] = 0;
                }
            }
            printVir(tempNum);// 打印首个辅助数组   
            list.add(c.createResult(a, tempNum, m));// 打印第一种默认组合   
            int k = 0;//标记位   
            while (!end) {
                boolean findFirst = false;
                boolean swap = false;
                // 然后从左到右扫描数组元素值的"10"组合,找到第一个"10"组合后将其变为"01"   
                for (int i = 0; i < n; i++) {
                    int l = 0;
                    if (!findFirst && tempNum[i] == 1) {
                        k = i;
                        findFirst = true;
                    }
                    if (tempNum[i] == 1 && tempNum[i + 1] == 0) {
                        tempNum[i] = 0;
                        tempNum[i + 1] = 1;
                        swap = true;
                        for (l = 0; l < i - k; l++) { // 同时将其左边的所有"1"全部移动到数组的最左端。   
                            tempNum[l] = tempNum[k + l];
                        }
                        for (l = i - k; l < i; l++) {
                            tempNum[l] = 0;
                        }
                        if (k == i && i + 1 == n - m) {//假如第一个"1"刚刚移动到第n-m+1个位置,则终止整个寻找   
                            end = true;
                        }
                    }
                    if (swap) {
                        break;
                    }
                }
                printVir(tempNum);// 打印辅助数组   
                list.add(c.createResult(a, tempNum, m));// 添加下一种默认组合   
            }
            return list;
        }
     
        // 根据辅助数组和原始数组生成结果数组   
        public int[] createResult(int[] a, int[] temp, int m) {
            int[] result = new int[m];
            int j = 0;
            for (int i = 0; i < a.length; i++) {
                if (temp[i] == 1) {
                    result[j] = a[i];
                    System.out.println("result[" + j + "]:" + result[j]);
                    j++;
                }
            }
            return result;
        }
     
        // 打印整组数组   
        public void print(List<int[]> list) {
            System.out.println("具体组合结果为:");
            for (int i = 0; i < list.size(); i++) {
                int[] temp = (int[]) list.get(i);
                for (int j = 0; j < temp.length; j++) {
                    java.text.DecimalFormat df = new java.text.DecimalFormat("00");//将输出格式化   
                    System.out.print(df.format(temp[j]) + " ");
                }
                System.out.println();
            }
        }
     
        // 打印辅助数组的方法   
        public void printVir(int[] a) {
            System.out.println("生成的辅助数组为:");
            for (int i = 0; i < a.length; i++) {
                System.out.print(a[i]);
            }
            System.out.println();
        }
     
        public static void main(String[] args) {
            int[] a = { 1, 2, 3, 4, 5 ,6,7,8,9}; // 整数数组   
            int m = 5; // 待取出组合的个数   
            AssemblyDemo c = new AssemblyDemo();
            List<int[]> list = c.combination(a, m);
            c.print(list);
            System.out.println("一共" + list.size() + "组!");
        }
    }