#CM10014. CSP-J 阅读理解模拟(四)

CSP-J 阅读理解模拟(四)

CSP-J 初赛阅读程序专项模拟(四)

二、阅读程序题(共3题,每题12分,共计36分)

阅读以下程序,回答后面的问题。判断题正确选A,错误选B。


程序一:

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

int cnt[26];

int main() {
    string s;
    cin >> s;
    for (int i = 0; i < s.length(); i++) {
        cnt[s[i] - 'a']++;
    }
    int ans = -1;
    for (int i = 0; i < s.length(); i++) {
        if (cnt[s[i] - 'a'] == 1) {
            ans = i;
            break;
        }
    }
    cout << ans << endl;
    return 0;
}

判断题(正确选A,错误选B)

1. 该程序的功能是统计字符串中每个字符出现的次数。( ){{ select(1) }}

  • 正确
  • 错误

2. 若输入字符串为 "loveleetcode",程序的输出为 3。( ){{ select(2) }}

  • 正确
  • 错误

3. 若输入字符串为 "aabb",程序的输出为 -1。( ){{ select(3) }}

  • 正确
  • 错误

选择题

4. 若输入字符串为 "leetcode",程序的输出为( )。{{ select(4) }}

  • 0
  • 1
  • 2
  • -1

5. 该程序中 cnt 数组的大小为 26,其作用是( )。{{ select(5) }}

  • 标记字符是否出现过
  • 统计每个小写字母出现的次数
  • 存储每个字符第一次出现的位置
  • 记录字符的ASCII码

6. 该程序的时间复杂度为( )。{{ select(6) }}

  • O(1)
  • O(n)
  • O(n²)
  • O(n log n)

程序二:

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

bool isMatch(char left, char right) {
    return (left == '(' && right == ')') ||
           (left == '[' && right == ']') ||
           (left == '{' && right == '}');
}

int main() {
    string s;
    cin >> s;
    stack<char> st;
    bool flag = true;
    for (int i = 0; i < s.length(); i++) {
        char ch = s[i];
        if (ch == '(' || ch == '[' || ch == '{') {
            st.push(ch);
        } else {
            if (st.empty() || !isMatch(st.top(), ch)) {
                flag = false;
                break;
            }
            st.pop();
        }
    }
    if (!st.empty()) flag = false;
    cout << (flag ? "true" : "false") << endl;
    return 0;
}

判断题(正确选A,错误选B)

7. 该程序的功能是判断字符串中的括号是否匹配,只包含 () 一种括号。( ){{ select(7) }}

  • 正确
  • 错误

8. 若输入字符串为 "({[]})",程序的输出为 true。( ){{ select(8) }}

  • 正确
  • 错误

9. 若输入字符串为 "([)]",程序的输出为 true。( ){{ select(9) }}

  • 正确
  • 错误

选择题

10. 若输入字符串为 "((()))",程序的输出为( )。{{ select(10) }}

  • true
  • false
  • 编译错误
  • 无法确定

11. 该程序中的 st.empty() 在程序结束时为 true 代表( )。{{ select(11) }}

  • 所有括号匹配成功
  • 存在多余的右括号
  • 存在多余的左括号
  • 字符串为空

12. 以下哪个字符串会使该程序输出 false 且程序结束时栈为空?( ){{ select(12) }}

  • "([)]"
  • "({)}"
  • "())"
  • "(()"

程序三:

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

int a[100005];

int main() {
    int n, d;
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];
    cin >> d;
    sort(a, a + n);
    int ans = 1, last = a[0];
    for (int i = 1; i < n; i++) {
        if (a[i] - last <= d) {
            ans++;
            last = a[i];
        }
    }
    cout << ans << endl;
    return 0;
}

判断题(正确选A,错误选B)

13. 该程序的功能是求数组的最长上升子序列长度。( ){{ select(13) }}

  • 正确
  • 错误

14. 若输入数组为 [1, 2, 3, 4, 5],d = 1,程序的输出为 5。( ){{ select(14) }}

  • 正确
  • 错误

15. 若输入数组为 [5, 1, 2, 3, 4],d = 1,程序的输出为 4。( ){{ select(15) }}

  • 正确
  • 错误

选择题

16. 该程序的核心算法思想是( )。{{ select(16) }}

  • 贪心
  • 分治
  • 动态规划
  • 枚举

17. 若输入数组为 [10, 1, 2, 3, 4, 5],d = 1,程序的输出为( )。{{ select(17) }}

  • 5
  • 6
  • 1
  • 3

18. 该程序中的变量 last 表示( )。{{ select(18) }}

  • 当前子序列的最后一个元素
  • 当前子序列的第一个元素
  • 当前遍历到的数组元素
  • 当前子序列的长度