笔试没弄出来,求解一下...
一个一维数组,里面有N个元素,从里面去R次,打印取出来的数的排列组合。取出顺序不管。
例如:[1,2,3] 取两次
可能取出的情况打印下来就是
1,2
2,3
1,3
求这个算法一下...
谢谢了,找工作不容易啊。

解决方案 »

  1.   


    public class Test {

    public static void main(String[] args) {
    int[] arr = new int[] { 1, 3, 5, 7, 9 };
    get(arr, 2);
    } public static void get(int[] a, int n) {
    if (n > a.length || n == 0) {
    return;
    }
    for (int i = n - 1; i < a.length; i++) {
    for (int j = 0; j < n - 1; j++) {
    System.out.print(a[j] + " ");
    }
    System.out.print(a[i] + "\n");
    }
    if (a.length - 1 < n || n == 1) {
    return;
    }
    int[] b = new int[a.length - 1];
    for (int i = 0; i < b.length; i++) {
    if (i == 0 && n != 2) {
    b[i] = a[i];
    } else {
    b[i] = a[i + 1];
    }
    }
    get(b, n);
    }
    }
      

  2.   


    import java.util.Random;public class Combination{

    int[] array={1,2,3};
    MyLinkedList newArray;
    boolean[] flags;
    int Cishu = 2;

    public Combination(int number){
    flags = new boolean[array.length];
    for(int i=0;i<array.length;i++){
    flags[i] = false;//表示该元素未取出
    }
    newArray = new MyLinkedList();
    this.Cishu = number;
    }
    public Combination(int[] array,int number){
    this.array = array;
    flags = new boolean[array.length];
    for(int i=0;i<array.length;i++){
    flags[i] = false;//表示该元素未取出
    }
    newArray = new MyLinkedList();
    this.Cishu = number;
    }

    public void getElement(){
    Random rd = new Random();
    int number = Math.abs(rd.nextInt()%array.length);
    while(flags[number]){//如果该数已经取出来了,丢弃,重取
    number = Math.abs(rd.nextInt()%array.length);
    }
    newArray.addElement(array[number]);
    flags[number] = true;
    }
    public void print(){
    for(int i=0;i<Cishu;i++){
    getElement();
    }
    Node node = newArray.head;
    while(node!=null){
    System.out.print(node.in+"|");
    node = node.next;
    }
    }
    public static void main(String args[]){

    new Combination(2).print();

    }
    }
    class MyLinkedList{
    Node head;
    Node tail;

    public void addElement(Integer in){
    if(head==null){
    Node node = new Node(in);
    head = node;
    tail = node;
    return;
    }
    if(head.in > in){
    Node node = new Node(in);
    head.prev = node;
    node.next = head;
    head = node;
    return;
    }
    Node zhizhen = head.next;
    while(zhizhen != null){
    if(zhizhen.in > in){//从小到大排序。
    Node node = new Node(in);
    zhizhen.prev.next = node;
    node.prev = zhizhen.prev;
    node.next = zhizhen;
    zhizhen.prev = node;
    return;
    }
    zhizhen = zhizhen.next;
    }
    Node node = new Node(in);//循环都没找到,说明是最大的
    tail.next = node;
    node.prev = tail;
    tail = node;
    }
    }
    class Node{//
    Integer in;
    Node next =null;//后向指针
    Node prev = null;//前向指针
    public Node(){

    }
    public Node(Integer in){
    this.in = in;
    this.next=null;
    this.prev=null;
    }
    }不好意思,我大概知道你的意思是要把所有的排列组合列出来,而我的程序是帮你模拟一个取数据的流程。尽管有点偏题,还是贴出来让你参考一下吧。
      

  3.   


    public class Test {
        int []data;
        int num;
        public Test(int []data,int num){
            this.data = data;
            this.getNum("",num);
        }
        public void getNum(String pre,int time){
            if(time==1)
            {
                for(int i=0;i<data.length;i++){
                    System.out.println(pre+data[i]);;
                }
            }else{
                int xx=time-1;
                for(int i=0;i<data.length;i++){
                    getNum(pre+data[i],xx);
                }
                time--;
            }
        }
        /**
         * @param args
         */
        public static void main(String[] args) {
            int aa[]=new int[]{1,2,3,4,5};
            new Test(aa,3);
        }
       
    }
      

  4.   

    那你改成3好了,那只是一个参数,随便多少的。
    get(arr, 3);
    一样能执行的吧
      

  5.   

    ....我试过了么~
    int[] arr = new int[] { 1, 2, 3, 4 };
            get(arr, 3);
    结果是:
    1 2 3
    1 2 4
    1 3 4
    没有2 3 4啊
      

  6.   


    LZ你好 
    3L用的2重循环提取 
    所以改成3之后再用2重循环只提取了含1的部分
    当然可以改成N重循环取N个 
    不过这不是个很好的方法...
      

  7.   

    for example
    public class Test {
        public static void main(String[] args) {
            combine(new int[]{1,2,3,4,5}, 3);
        }    public static void combine(int[] a, int n) {
            if (n > a.length) {
                for (int i : a) {
                    System.out.printf("%d ", i);
                }
                return;
            }        int idx=0, t=0, p=(int)Math.pow(2, a.length);
            while (p > 0) {
                if (Integer.bitCount(p) == n) {
                    t = p;
                    idx = 0;
                    while (t > 0) {
                        if (t%2==1) {
                            System.out.printf("%d ", a[idx]);
                        }
                        t >>= 1;
                        idx++;
                    }
                    System.out.println();
                }
                p--;
            }
        }
    }
      

  8.   

    在3楼上 基础改进   这样代码应该符合你的要求  
    如果数组不是个有序数组  请先排序后在运行
        public static void main(String[] args) {
            int[] arr = new int[] { 1, 3, 5, 7, 9 };
           int leng=arr.length;
            int n=2;
            for(int i=0;i<leng-n;i++){
              int [] count =arr;
              get(arr, n);
              arr = new int [arr.length-1];
              for(int k =0;k<arr.length;k++){
              arr[k]=count[k+1];
              }
            
              
            
            }
           for(int i=0;i<arr.length;i++){
            System.out.print(arr[i]+" ");
           }
           
        }    public static void get(int[] a, int n) {
            if (n > a.length || n == 0) {
                return;
            }
            for (int i = n - 1; i < a.length; i++) {
                for (int j = 0; j < n - 1; j++) {
                    System.out.print(a[j] + " ");
                }
                System.out.print(a[i] + "\n");
            }
            if (a.length - 1 < n || n == 1) {
                return;
            }
            int[] b = new int[a.length - 1];
            for (int i = 0; i < b.length; i++) {
                if (i == 0 && n != 2) {
                    b[i] = a[i];
                } else {
                    b[i] = a[i + 1];
                }
            }
            get(b, n);
        }
           
        }
      

  9.   


    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;public class Combination{

    int[] array={1,2,3};
    int number = 2;
    static List<Integer> list= new ArrayList<Integer>();
    List<List<Integer>> answers = new ArrayList<List<Integer>>();

    public Combination(int number){
    this.number = number;
    }
    public Combination(int[] array,int number){
    this.array = array;
    this.number = number;
    for(int j=0;j<array.length;j++){
    list.add(array[j]);
    }

    }

    public void print(List<Integer> list){
    for(Iterator<Integer> it = list.iterator();it.hasNext();){
    System.out.print(it.next() +" ");

    }
    System.out.println("");
    }
    public void doSomthing(List list,int number){
    if(number==this.number){
    print(list);
    return;
    }
    for(int i=0;i<list.size();i++){
    Integer temp = (Integer)list.get(i);
    list.remove(i);
    doSomthing(list,number-1);
    list.add(i, temp);
    }
    }

    public static void main(String args[]){

    new Combination(new int[]{1,2,3,4,5},3).doSomthing(list, 5);
    }
    }//楼主我尽力了,你在改进改进,思想摆哪里,11楼我没仔细,好像用到移位,这个真不懂
      

  10.   


    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Set;public class Combination{

    int[] array={1,2,3};
    int number = 2;
    List<Integer> list= new ArrayList<Integer>();
    Set<List<Integer>> answers = new HashSet<List<Integer>>();

    public Combination(int number){
    this.number = number;
    }
    public Combination(int[] array,int number){
    this.array = array;
    this.number = number;
    for(int j=0;j<array.length;j++){
    list.add(array[j]);
    }
    }

    public void print(List<Integer> list){
    for(Iterator<Integer> it = list.iterator();it.hasNext();){
    System.out.print(it.next() +" ");
    }
    System.out.println("");
    }
    public void doSomthing(List<Integer> list,int number){
    if(number==this.number){
    List<Integer> list1=new ArrayList<Integer>();
    list1.addAll(list);
    answers.add(list1);
    return;
    }
    for(int i=0;i<list.size();i++){
    Integer temp = list.get(i);
    list.remove(i);
    doSomthing(list,number-1);
    list.add(i, temp);
    }
    }
    public void combine(){
    doSomthing(list,list.size());
    for(Iterator<List<Integer>> it=answers.iterator();it.hasNext(); ){
    print(it.next());
    }
    }

    public static void main(String args[]){
    new Combination(new int[]{1,2,3,4,5},3).combine();
    }
    }
    //修正bug去重复问题,思路用到递归,当n为数组取n个数时,只有一种情况,当取n-1时有n种情况,然后用递//归了。写这个程序本人表示鸭梨很大。修正3次才得结果