从一系列数中选取n个数做为一个组合,得到全部的组合并输出

组合类Combinations.java

package com.zr.demo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/** @version: 1.0.0
 * @Description: 生成所有组合并输出它们
 * @author: ZR
 * @date: 2019年12月25日 下午10:04:00
 */
public class Combinations {

	/**
	 * 从一系列数中生成所有组合并输出它们
	 * @param selected 选择的组合
	 * @param data 数据
	 * @param n 每个组合中元素个数
	 */
	public void generateCombinations(List<Integer> selected,List<Integer> data,int n) {
		if (n==0) {
			//输出组合中所有的元素
			for (Integer i : selected) {
				System.out.print(i+" ");
			}
			System.out.println();
			return;
		}
		
		if (data.isEmpty()) {
			return;
		}
		
		//将第一个元素添加到组合
		selected.add(data.get(0));
		//在除去第一个元素的数据中新生成包含(n-1)个元素的组合
		generateCombinations(selected, data.subList(1, data.size()), n-1);
	
		//将最后一个元素从组合中删除
		selected.remove(selected.size()-1);
		//在除去第一个元素的数据中生成包含n个元素的组合
		generateCombinations(selected, data.subList(1, data.size()), n);
	
	}
	
	
	public static void main(String[] args) {
		Combinations combinations=new Combinations();
		
		System.out.println("Testing normal data:");
		System.out.println("*************************");
		combinations.generateCombinations(new ArrayList<Integer>(), Arrays.asList(1,2,3,4), 2);
		System.out.println("=========================");
		
		System.out.println("Testing empty data:");
		System.out.println("*************************");
		combinations.generateCombinations(new ArrayList<Integer>(), new ArrayList<Integer>(), 2);
		System.out.println("=========================");
		System.out.println("*************************");
		combinations.generateCombinations(new ArrayList<Integer>(), new ArrayList<Integer>(), 0);
		System.out.println("=========================");
		
		System.out.println("Selecting 1 and 0 elements:");
		System.out.println("*************************");
		combinations.generateCombinations(new ArrayList<Integer>(), Arrays.asList(1,2,3,4), 1);
		System.out.println("=========================");
		System.out.println("*************************");
		combinations.generateCombinations(new ArrayList<Integer>(), Arrays.asList(1,2,3,4), 0);
		System.out.println("=========================");
		
		System.out.println("Testing larger data and selected:");
		System.out.println("*************************");
		combinations.generateCombinations(new ArrayList<Integer>(), Arrays.asList(1,2,3,4,5,6,7,8,9,10), 4);
		System.out.println("=========================");
	}

}

输出结果:

Testing normal data:
*************************
1 2 
1 3 
1 4 
2 3 
2 4 
3 4 
=========================
Testing empty data:
*************************
=========================
*************************

=========================
Selecting 1 and 0 elements:
*************************
1 
2 
3 
4 
=========================
*************************

=========================
Testing larger data and selected:
*************************
1 2 3 4 
1 2 3 5 
1 2 3 6 
1 2 3 7 
1 2 3 8 
1 2 3 9 
1 2 3 10 
1 2 4 5 
1 2 4 6 
1 2 4 7 
1 2 4 8 
1 2 4 9 
1 2 4 10 
1 2 5 6 
1 2 5 7 
1 2 5 8 
1 2 5 9 
1 2 5 10 
1 2 6 7 
1 2 6 8 
1 2 6 9 
1 2 6 10 
1 2 7 8 
1 2 7 9 
1 2 7 10 
1 2 8 9 
1 2 8 10 
1 2 9 10 
1 3 4 5 
1 3 4 6 
1 3 4 7 
1 3 4 8 
1 3 4 9 
1 3 4 10 
1 3 5 6 
1 3 5 7 
1 3 5 8 
1 3 5 9 
1 3 5 10 
1 3 6 7 
1 3 6 8 
1 3 6 9 
1 3 6 10 
1 3 7 8 
1 3 7 9 
1 3 7 10 
1 3 8 9 
1 3 8 10 
1 3 9 10 
1 4 5 6 
1 4 5 7 
1 4 5 8 
1 4 5 9 
1 4 5 10 
1 4 6 7 
1 4 6 8 
1 4 6 9 
1 4 6 10 
1 4 7 8 
1 4 7 9 
1 4 7 10 
1 4 8 9 
1 4 8 10 
1 4 9 10 
1 5 6 7 
1 5 6 8 
1 5 6 9 
1 5 6 10 
1 5 7 8 
1 5 7 9 
1 5 7 10 
1 5 8 9 
1 5 8 10 
1 5 9 10 
1 6 7 8 
1 6 7 9 
1 6 7 10 
1 6 8 9 
1 6 8 10 
1 6 9 10 
1 7 8 9 
1 7 8 10 
1 7 9 10 
1 8 9 10 
2 3 4 5 
2 3 4 6 
2 3 4 7 
2 3 4 8 
2 3 4 9 
2 3 4 10 
2 3 5 6 
2 3 5 7 
2 3 5 8 
2 3 5 9 
2 3 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8 9 10 
7 8 9 10 
=========================
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值