问题是从1,2,。。。。。n中选取有r个数的组合。
应用递归思想,
先从n个数中选取一个,接下来便是从n-1个选取r-1个,一直递归到取完为止。
代码如下:
应用递归思想,
先从n个数中选取一个,接下来便是从n-1个选取r-1个,一直递归到取完为止。
代码如下:
package Zuhe;
public class Zuhe {
public int[] a=new int[100];
public static void main(String[] args) {
Zuhe zh=new Zuhe();
zh.combile(5, 3);
}
public void combile(int n,int r){
int i,j;
for(i=n;i>=r;i--){ //i代表的选取的数里最大的数,最大数放在a[r]
a[r]=i;
if(r==1){
for(j=1;a[j]>0;j++)
System.out.print(a[j]+" ");
System.out.println();
continue;
}
else
combile(i-1,r-1);
}
}
}