hello world

随笔 - 2, 文章 - 63, 评论 - 0, 引用 - 0
数据加载中……

求取数列的排列。利用递归

				 public class Order2 {
static int num=1;

private boolean isIn(int a[],int start,int num){
for(int i=0;i<start;i++){
if(a[i]==num)
return true;
}
return false;
}

public void orderNumber(int[] a, int start, int deep, int length, int k) {
int passNum[]=new int[length];
int passLength=0;
for (int i = start; i < length; i++) {
if(isIn(passNum,passLength,a[i]))
continue;

passNum[passLength]=new Integer(a[i]).intValue();
passLength++;

int t = a[start];
a[start] = a[i];
a[i] = t;

if (deep == 1) {
System.out.print(num+++" ");
for (int j = 0; j < k; j++)
System.out.print(a[j]);
System.out.println();
} else
orderNumber(a, start + 1, deep - 1, length, k);

t = a[start];
a[start] = a[i];
a[i] = t;
}

}

public static void main(String arg[]){
int a[]={1,2,2,4,5};
int start=0;
int deep=3;
int length=5;
new Order2().orderNumber(a, start, deep, length,deep);
}
}

posted on 2009-12-08 12:16 听风 阅读(190) 评论(0)  编辑  收藏 所属分类: JAVA


只有注册用户登录后才能发表评论。


网站导航: