#5211. C++ 程序阅读 · 强生挑战

C++ 程序阅读 · 强生挑战

C++ 程序阅读 · 强生挑战

完成进阶摸底与复杂度专项后继续。建议限时 12 分钟。


第 1 题

int x = 2026, cnt = 0;
while (x) {
    x &= (x - 1);
    cnt++;
}
cout << cnt;

{{ select(1) }}

  • 6
  • 7
  • 8
  • 9

第 2 题

stack<int> s;
queue<int> q;
for (int i = 1; i <= 4; i++) {
    s.push(i);
    if (i % 2 == 0) {
        q.push(s.top());
        s.pop();
    }
}
while (!s.empty()) {
    q.push(s.top());
    s.pop();
}
while (!q.empty()) {
    cout << q.front();
    q.pop();
}

{{ select(2) }}

  • 2413
  • 2431
  • 4213
  • 4321

第 3 题

int a[] = {4, 1, 7, 2, 6};
int ans = 0;
for (int i = 0; i < 5; i++)
    for (int j = i + 1; j < 5; j++)
        if ((a[i] ^ a[j]) & 1) ans++;
cout << ans;

{{ select(3) }}

  • 4
  • 5
  • 6
  • 7

第 4 题

int f(int a, int b) {
    return b ? f(b, a % b) : a;
}
cout << f(252, 105);

{{ select(4) }}

  • 7
  • 14
  • 21
  • 42

第 5 题

vector<int> a = {2, 5, 8, 12, 16, 23, 38};
int l = 0, r = 6, ans = -1;
while (l <= r) {
    int mid = (l + r) / 2;
    if (a[mid] >= 15) {
        ans = mid;
        r = mid - 1;
    } else {
        l = mid + 1;
    }
}
cout << ans << " " << a[ans];

{{ select(5) }}

  • 3 12
  • 4 16
  • 5 23
  • 6 38

第 6 题

int a[] = {3, 1, 4, 1, 5};
int x = 0;
for (int v : a) x ^= v;
cout << x;

{{ select(6) }}

  • 0
  • 1
  • 2
  • 3