首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

数组装N个元素,取R次,打印所有排列。求算法,该怎么处理

2012-04-09 
数组装N个元素,取R次,打印所有排列。求算法笔试没弄出来,求解一下...一个一维数组,里面有N个元素,从里面去R

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

[解决办法]

Java code
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);    }}
[解决办法]
Java code
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;    }}
[解决办法]
for example
Java code
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--;        }    }} 


[解决办法]
在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);
}



}
[解决办法]

Java code
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次才得结果 

热点排行