急求一个c(n,r)的组合排序
c(n, r)
我输入(4, 2)
意思是在1,2,3,4中任取2个.打印如下:
1,2
1,3
1,4
2,3
2,4
3,4
我输入(5, 3)
意思是在1,2,3,4中任取2个.打印如下:
1,2,3
1,2,4
1,2,5
1,3,4
1,3,5
1,4,5
2,3,4
2,3,5
2,4,5
3,4,5
打印顺序需要按上面的排序.
谢谢.
问题点数:60、回复次数:4Top
1 楼treeroot(旗鲁特)回复于 2006-03-16 18:56:42 得分 0
什么顺序?Top
2 楼wizardblue()回复于 2006-03-16 18:57:05 得分 60
public class CMN {
private int maxIndex = 4;// or 5;
private int top = 26;
private static int count = 0;
private int[] s;
public static void main(String[] args) {
CMN cmn = new CMN(2,4);
cmn.DFS(0, 0);
cmn = new CMN(3,5);
cmn.DFS(0, 0);
}
public CMN(int max,int top){
this.maxIndex = max;
this.top = top ;
s = new int[maxIndex];
}
public void DFS(int now, int index) {
for (int i = now; i < top; i++) {
s[index] = i+1;
if (index == maxIndex- 1) {
show(s);
count++;
} else {
DFS(i + 1, index + 1);
}
}
}
private static void show(int[] s) {
for (int i = 0; i < s.length; i++) {
System.out.print(s[i]);
}
System.out.println();
}
}
Top
3 楼gmlxf(烛光)回复于 2006-03-16 21:37:01 得分 0
非常感谢.可以了.
代码真够简洁,思路不错.Top
4 楼TONYBLARED(奔放的犀牛)回复于 2006-03-16 23:22:37 得分 0
wizardblue(不死鱼) 是个强人.有机会多交流哈.Top




