组合

package L4;

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

public class L9 {

	static char[] c = new char[3];
	static int in = 0;

	// 组合枚举
	// index 元素位置
	// k 选取的元素个数
	// arr 数组
	static void com(int index, int k, char[] arr) {

		if (k == 0) {
			System.out.println(Arrays.toString(c));
			return;
		}
		for (int i = index; i < arr.length; i++) {
			c[in++] = arr[i];//暂时存储元素
			com(i + 1, k - 1, arr);//从选取得元素后面选取下一个,避免重复
			in--;
			c[in] = '0';//回溯
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		char[] s = "ABCD".toCharArray();
		com(0, 3, s);
	}

}

public class CB7 {

	static int[] nums = { 1, 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, 14 };
	static int[] c = new int[5];
	static int cnt = 0;

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		cbt(0, 5);
		System.out.println("cnt:" + cnt);
	}


	public static boolean isok() {

	}

	// low代表起始位,num代表取出的数字
	public static void cbt(int low, int num) {

		// 出口
		if (num == 0) {
			// 条件判断

			if (isok()) {
				cnt++;
			}
			return;
		}

		for (int i = low; i < nums.length; i++) {

			c[num - 1] = nums[i];
			cbt(i + 1, num - 1);
		}

	}
}
	static int n;// 从n个数里选出m个数
	static int m;

	static void cbt() {
		int cntm = 0;
		for (int i = 0; i < (1 << n); i++) {
			for (int j = 0; j < n; j++) {
				if ((1 & (1 << j)) == 1) {
					// 这个位置选中
					cntm++;
				}

			}
			if (cntm == m) {
				// 选出的结果
			}
		}
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值