wanran 发表于 2013-1-26 13:59:22

生成排列数(java&c++)

用递归的方法生成一列数的所有排列组合

public class Main {public static void main(String[] args) {Integer[] a = {1, 2, 3, 4};perm(a, 0, 3);}public static void perm(Object[] list, int k, int m) {if(k==m) {for(int i=0; i<=m; i++)System.out.print(list);System.out.println();}elsefor(int i=k; i<=m; i++) {swap(list, k, i);perm(list, k+1, m);swap(list, k, i);}}public static void swap(Object[] list, int k, int m) {Object temp;temp = list;list = list;list = temp;}}



void swap(int* list , int k, int m) {    int temp;    temp = list;    list = list;    list = temp;}void perm(int list[], int k, int m) {    if(k==m) {      for(int i=0; i<=m; i++) {            cout<<list;      }      cout<<endl;    }    else {      for(int i=k; i<=m; i++) {            swap(list, k, i);            perm(list , k+1, m);            swap(list, k, i);      }    }}
页: [1]
查看完整版本: 生成排列数(java&c++)