组合
import java.util.Scanner; public class Main { static int n,k; static int [] a; static void combination() { int nbit = 1 << n; System.out.println(Integer.toBinaryString(nbit)); for (int i = 0; i < nbit; i++) { int sum = 0; for (int j = 0; j < n; j++) { int tmp = 1 << j; // 由0到n右移位 if ((tmp & i) != 0) { // 与运算,同为1时才会是1 sum += a[j]; } } if(sum == k){ System.out.println("YES"); break; } } } public static void main(String[] args) { Scanner in = new Scanner(System.in); n = in.nextInt(); k = in.nextInt(); a = new int[n]; for(int i = 0; i < n; i++){ a[i] = in.nextInt(); } combination(); } }