您的位置:首页 > 其它

第四讲 递归原理--数组的全排列(递归+交换)

2014-12-04 15:08 197 查看
package chapter4;
/**
* @功能说明 :
* 实现一个数组的全排列
* 这里使用了交换+递归法
* @author shaochong
* @Date  2014年12月4日 下午3:06:01
*/
public class pailie
{
static void show(char[] a){
for(int i=0; i<a.length; i++) System.out.print(a[i]+" ");
System.out.println();
}

static void f(char[] a, int k)
{
if(k==a.length-1){
show(a);
return;
}
for(int i=k; i<a.length; i++){
{char t = a[k]; a[k]=a[i]; a[i]=t;}
f(a,k+1);
{char t = a[k]; a[k]=a[i]; a[i]=t;}
}
}
public static void main(String[] args)
{
char[] a = "ABCD".toCharArray();
f(a,0);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: