#12267. 珅泽教育CSP-J第一轮模拟考第二十四套 第 39 题

珅泽教育CSP-J第一轮模拟考第二十四套 第 39 题

三、完善程序(单选题,每小题 3 分,共计 30 分)

程序二:凑出 17

给定 n(1n20)n(1\le n\le20) 个互不相同的正整数 a1,,an(1ai109)a_1,\ldots,a_n(1\le a_i\le10^9),在每个数前添加加号或减号,判断算式的值能否为 17;存在输出 Yes,否则输出 No

#include <cstdio>

using namespace std;

const int maxn = 25;
const int aim = 17;

int n;
int a[maxn];
bool ans;

int getBit(const int s, int p) {
    return ①;
}

int main() {
    scanf("%d", &n);
    for (②) scanf("%d", a + i);
    for (int s = 0, upperBound = ③; s <= upperBound; ++s) {
        ④;
        for (int j = 0; j < n; ++j) if (getBit(s, j) == 1) {
            sum += a[j];
        } else {
            ⑤;
        }
        if (int(sum) == aim) {
            ans = true;
            break;
        }
    }
    printf("%s\n", ans ? "Yes" : "No");
}
  1. ①处应填( )。

{{ select(1) }}

  • (s >> p) & 1
  • (s << p) & 1
  • s & (1 << p) & 1
  • s & (1 >> p) & 1