package com.test;
/**
* @ClassName TestLk
* @Description 从 N 个数中,取 M 个数的组合(枚举),要求不重复;比如:彩票 36 选 7
* 2、3、7、11、20、22、31
* 3、2、7、11、20、22、31
* 视为相同的一组
* @Author lk
* @Date 2022/2/16 12:19
* @Version 1.0
**/
public class TestLk {
static int N = 6;
static int M = 3;
static int[] a = new int[]{
1, 2, 3, 4, 5, 6};
static int[] b = new int[M];
public static void main(String[] args){
C(N,M);
}
// 递归法
static void C(int m,int n){
int i,j;
for(i=n;i<=m;i++) {
b[n-1] = i-1; // 计算a数组的下标
if(n>1)
C(i-1,n-1); // 当b数组的下标大于1,进行递归,否则:输出一组值
else {
// for(int a:b)
// System.out.println(a);
for(j=0;j<=M-1;j++)
if(j == (M-1)){
System.out.print(a[b[j]]);
}else{
System.out.print(a[b[j]] + "、");
}
System.out.println();
}
}
}
}
输出结果:
1、2、3
1、2、4
1、3、4
2、3、4
1、2、5
1、3、5
2、3、5
1、4、5
2、4、5
3、4、5
1、2、6
1、3、6
2、3、6
1、4、6
2、4、6
3、4、6
1、5、6
2、5、6
3、5、6
4、5、6
从 N 个数中,取 M 个数的组合(枚举),要求不重复
猜你喜欢
转载自blog.csdn.net/weixin_43317914/article/details/122969172
今日推荐
周排行