编写一个程序,读取由 n 个元素组成的序列 A 和一个整数 M,如果可以通过在 A 中的元素相加得到 M,则输出“yes”,否则输出“no”。 一个元素只能使用一次。
给出序列 A 和 q 个问题,其中每个问题都包含Mi
输入
在第一行中给出了 n。 在第二行中,给出了 n 个整数。 在第三行中给出了 q。 然后,在第四行,给出 q 个(Mi)。
输出
对于每个Mi打印"yes"或"no"。
约束
n ≤ 20
q ≤ 200
1 ≤ A 中的元素 ≤ 2000
1 ≤Mi≤ 2000
输入样例
5 1 5 7 10 21 8 2 4 17 8 22 21 100 35
输出样例
no no yes yes yes yes no no
#include<bits/stdc++.h>
using namespace std;
bool f[2002];
short num[2002];
int Tail;
int main()
{
int n, a, x;
int stop;
num[0] = 0; //非常重要
Tail = 1;
cin>>n;
while (n--) {
stop = Tail;
cin>>a;
for (int i = 0; i < stop; i++) {
x = num[i] + a;
if (x>2000 || f[x]) continue;
f[x] = 1;
num[Tail++] = x;
}
}
int q;
cin>>q;
while (q--) {
cin>>x;
puts(f[x]? "yes": "no");
}
return 0;
}