public static void qpl(char[] a,int begin,int end){ if(begin==end-1){ for(int j=0;j<end;j++) System.out.print(a[j]); System.out.println(); } else{ for(int i=begin;i<end;i++){ char tmp=a[i]; a[i]=a[begin]; a[begin]=tmp; qpl(a,begin+1,end); tmp=a[i]; a[i]=a[begin]; a[begin]=tmp; } } } public static void main(String[] args) { // TODO Auto-generated method stub char[] a={'1','2','3','4'}; System.out.print("全排列为:"); qpl(a,0,a.length); } }
结果:全排列为:1234
1243
1324
1342
1432
1423
2134
2143
2314
2341
2431
2413
3214
3241
3124
3142
3412
3421
4231
4213
4321
4312
4132
4123
参考博客:http://blog.youkuaiyun.com/wuzhekai1985/article/details/6643127