#CM10026. CSP-J 完善程序模拟(六)

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

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

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

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


题目一:

【问题描述】

给定一个行程长度编码(Run-Length Encoding, RLE)压缩后的字符串,请将其解码还原为原始字符串。RLE 编码格式为:数字 + 字符,表示将该字符重复数字次。数字可能为 1 位或多位(如 12a 表示 12 个 'a')。

例如:

  • 输入:"3a2b1c" → 输出:"aaabbc"
  • 输入:"12a3b" → 输出:"aaaaaaaaaaaabbb"(12个a + 3个b)

【算法原理】

  1. 遍历编码字符串,提取连续的数字字符组成一个整数 count
  2. 遇到字母时,将当前累计的 count 作为重复次数,将该字母重复 count 次加入结果
  3. 每次遇到字母后,将 count 重置为 0
  4. 注意:如果数字部分缺失(即编码以字母开头),默认 count = 1

【程序】

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

string decodeRLE(string s) {
    string result = "";
    int n = s.length();
    int count = 0;
    for (int i = 0; i < n; i++) {
        char ch = s[i];
        if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
            if (count == 0) count = 1;
            for (int j = 0; j < count; j++) result += ch;
            count = 0;
        } else {
            count = count * 10 + (ch - '0');
        }
    }
    return result;
}

int main() {
    string s;
    cin >> s;
    cout << decodeRLE(s) << endl;
    return 0;
}

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

  • ch >= 'a' && ch <= 'z'
  • ch >= 'A' && ch <= 'Z'
  • ch >= '0' && ch <= '9'
  • (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')

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

  • count = count * 10 + (ch - '0')
  • count = count + (ch - '0')
  • count = count * 10 + ch
  • count = ch - '0'

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

  • count = 1
  • count = 0
  • count = max(1, count)
  • count = count + 1

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

  • while (cnt--) result += ch
  • for (int i = 0; i < count; i++) result += ch
  • result.append(count, ch)
  • result = result + string(count, ch)

提示:C++ 中 string 类没有 append(count, ch) 的成员函数(参数顺序相反),但有 append(size_t count, char ch) 的成员函数。


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

  • return result
  • cout << result
  • return 0
  • cout << result << endl

题目二:

【问题描述】

给定一个包含 n 个互不相同的整数数组 a,请输出从这 n 个数中选出 k 个数的所有不同组合。组合内元素按升序排列,不同组合之间按字典序升序输出。

例如:

  • 输入:n = 4, k = 2, a = [1, 2, 3, 4]
  • 输出:[1,2] [1,3] [1,4] [2,3] [2,4] [3,4]

【算法原理】

  1. 先将数组按升序排序,保证组合内元素升序
  2. 使用递归 DFS 枚举所有组合
  3. 每次递归选择一个元素加入当前组合,然后从该元素的下一个位置继续选择
  4. 当已选元素个数等于 k 时,输出当前组合
  5. 注意剪枝:剩余元素不足以凑成 k 个时提前结束

【程序】

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

int a[1005];
vector<int> path;
int n, k;

void dfs(int depth, int start) {
    if (depth == k) {
        for (int i = 0; i < k; i++) {
            cout << path[i] << " ";
        }
        cout << endl;
        return;
    }
    for (int i = start; i <= n - (k - depth); i++) {
        path.push_back(a[i]);
        dfs(depth + 1, i + 1);
        path.pop_back();
    }
}

int main() {
    cin >> n >> k;
    for (int i = 0; i < n; i++) cin >> a[i];
    sort(a, a + n);
    dfs(0, 0);
    return 0;
}

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

  • a + 1, a + n + 1
  • a, a + n
  • a + 1, a + n
  • a, a + n + 1

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

  • pos + 1
  • pos
  • start
  • start + 1

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

  • i < n
  • i <= n
  • i <= n - (k - depth)
  • i < n - (k - depth)

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

  • path.push_back(a[i])
  • path[depth] = a[i]
  • path.push_back(i)
  • path[depth] = i

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

  • dfs(depth + 1, i)
  • dfs(depth + 1, i + 1)
  • dfs(depth, i + 1)
  • dfs(depth + 1, start)