#CM10027. CSP-J 完善程序模拟(七)

CSP-J 完善程序模拟(七)

CSP-J 初赛完善程序专项模拟(七)

三、完善程序题(共2题,每题5空,每空3分,共计30分)

根据题目说明和已有代码,在空缺处填入正确的语句。每题均为四选一单选题。


题目一:

【问题描述】

给定两个非负整数 a 和 b(a, b ≤ 10^9),请使用扩展欧几里得算法求出一组整数解 (x, y),使得:

a × x + b × y = gcd(a, b)

如果有多组解,输出任意一组即可。

例如:

  • 输入:a = 30, b = 18
  • gcd(30, 18) = 6
  • 输出:x = -1, y = 2,因为 30×(-1) + 18×2 = 6

【算法原理】

  1. 扩展欧几里得算法在递归计算 gcd(a, b) 的同时,回溯计算系数 x 和 y
  2. 递归边界:当 b = 0 时,gcd = a,此时 x = 1,y = 0
  3. 递归关系:设 gcd(a, b) = gcd(b, a % b)
  4. 若 b × x1 + (a % b) × y1 = gcd,则:
    • x = y1
    • y = x1 - (a / b) × y1

【程序】

#include <iostream>
using namespace std;

int exgcd(int a, int b, int &x, int &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    int x1, y1;
    int g = exgcd(b, a % b, x1, y1);
    x = y1;
    y = x1 - (a / b) * y1;
    return g;
}

int main() {
    int a, b, x, y;
    cin >> a >> b;
    int g = exgcd(a, b, x, y);
    cout << "gcd(" << a << ", " << b << ") = " << g << endl;
    cout << "x = " << x << ", y = " << y << endl;
    cout << a << " * " << x << " + " << b << " * " << y << " = " << g << endl;
    return 0;
}

1. 空缺 (1) 处应填入( )。{{ select(1) }}

  • exgcd(a, a % b, x, y)
  • exgcd(b, a % b, x, y)
  • exgcd(a, b, x, y)
  • exgcd(a % b, b, x, y)

2. 空缺 (2) 处应填入( )。{{ select(2) }}

  • x = y1
  • y = x1
  • x = x1
  • y = y1

3. 空缺 (3) 处应填入( )。{{ select(3) }}

  • y = x1 - (a / b) * y1
  • y = y1 - (a / b) * x1
  • x = x1 - (a / b) * y1
  • y = x1 + (a / b) * y1

4. 空缺 (4) 处应填入( )。{{ select(4) }}

  • x = 0, y = 1
  • x = 1, y = 0
  • x = 1, y = 1
  • x = 0, y = 0

5. 空缺 (5) 处应填入( )。{{ select(5) }}

  • return exgcd(a, b, x, y)
  • return b
  • return a
  • return 0

题目二:

【问题描述】

给定一个长度为 n 的整数数组,找出所有出现次数超过 ⌊n/3⌋ 次的元素(众数 II)。返回这些元素的列表,顺序不限。

例如:

  • 输入:[3, 2, 3],n = 3,⌊3/3⌋ = 1,超过 1 次的元素为 [3]
  • 输入:[1, 1, 1, 3, 3, 2, 2, 2],n = 8,⌊8/3⌋ = 2,超过 2 次的元素为 [1, 2]

【算法原理】

  1. 摩尔投票算法的扩展:n/3 众数最多只有 2 个
  2. 维护两个候选元素 candidate1、candidate2 和对应的计数器 count1、count2
  3. 第一遍遍历:投票阶段
    • 若与 candidate1 相同,count1++
    • 否则若与 candidate2 相同,count2++
    • 否则若 count1 == 0,设 candidate1 = 当前元素,count1 = 1
    • 否则若 count2 == 0,设 candidate2 = 当前元素,count2 = 1
    • 否则 count1--,count2--(抵消)
  4. 第二遍遍历:验证阶段,统计两个候选元素实际出现次数
  5. 将出现次数 > n/3 的候选元素加入结果

【程序】

#include <iostream>
#include <vector>
using namespace std;

vector<int> majorityElement(vector<int>& nums) {
    int n = nums.size();
    int candidate1 = 0, candidate2 = 0;
    int count1 = 0, count2 = 0;
    for (int num : nums) {
        if (num == candidate1) {
            count1++;
        } else if (num == candidate2) {
            count2++;
        } else if (count1 == 0) {
            candidate1 = num;
            count1 = 1;
        } else if (count2 == 0) {
            candidate2 = num;
            count2 = 1;
        } else {
            count1--;
            count2--;
        }
    }
    int cnt1 = 0, cnt2 = 0;
    for (int num : nums) {
        if (num == candidate1) cnt1++;
        else if (num == candidate2) cnt2++;
    }
    vector<int> ans;
    if (cnt1 > n / 3) ans.push_back(candidate1);
    if (cnt2 > n / 3) ans.push_back(candidate2);
    return ans;
}

int main() {
    int n;
    cin >> n;
    vector<int> nums(n);
    for (int i = 0; i < n; i++) cin >> nums[i];
    vector<int> ans = majorityElement(nums);
    for (int x : ans) cout << x << " ";
    cout << endl;
    return 0;
}

6. 空缺 (1) 处应填入( )。{{ select(6) }}

  • count1 == 0 && nums[i] != candidate2
  • count1 == 0
  • nums[i] == candidate1
  • count2 == 0

7. 空缺 (2) 处应填入( )。{{ select(7) }}

  • count2++
  • count1++
  • count1--
  • count2--

8. 空缺 (3) 处应填入( )。{{ select(8) }}

  • count1--, count2--
  • count1++, count2++
  • count1 = 0, count2 = 0
  • count1--, count2 = 0

9. 空缺 (4) 处应填入( )。{{ select(9) }}

  • nums[i] == candidate1
  • nums[i] == candidate2
  • nums[i] == candidate1 || nums[i] == candidate2
  • nums[i] == candidate1 && nums[i] == candidate2

10. 空缺 (5) 处应填入( )。{{ select(10) }}

  • cnt1 > n / 3
  • cnt1 >= n / 3
  • cnt1 > n / 3 && candidate1 != candidate2
  • cnt1 > n / 3 && cnt2 > n / 3