#CM10019. CSP-J 阅读理解模拟(九)

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

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

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

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


程序一:

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

int gcd(int a, int b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}

int main() {
    int n;
    scanf("%d", &n);
    int ans = 0;
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n; ++j) {
            if (gcd(i, j) == 1) ans++;
        }
    }
    printf("%d\n", ans);
    return 0;
}

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

1. 该程序的功能是统计 [1, n] 范围内所有满足 gcd(i, j) = 1 的数对 (i, j) 的个数,其中 i 和 j 可以相等。( ){{ select(1) }}

  • 正确
  • 错误

2. 若输入 n = 1,程序的输出为 1。( ){{ select(2) }}

  • 正确
  • 错误

3. 若将第13行的 if (gcd(i, j) == 1) ans++; 改为 if (gcd(i, j) > 1) ans++;,当 n = 5 时,输出会大于原输出。( ){{ select(3) }}

  • 正确
  • 错误

选择题

4. 若输入 n = 4,程序的输出为( )。{{ select(4) }}

  • 8
  • 9
  • 10
  • 11

5. 调用 gcd(18, 24) 会返回( )。{{ select(5) }}

  • 2
  • 3
  • 6
  • 8

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

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

程序二:

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

int a[200005];

int main() {
    int n, k;
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
    sort(a + 1, a + n + 1);
    n = unique(a + 1, a + n + 1) - a - 1;
    int j = 1;
    long long ans = 0;
    for (int i = 1; i <= n; ++i) {
        while (j < i && a[i] - a[j] >= k) ++j;
        ans += i - j;
    }
    printf("%lld\n", ans);
    return 0;
}

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

7. 该程序的功能是统计排序去重后,有多少个区间 [l, r] 满足 a[r] - a[l] < k。( ){{ select(7) }}

  • 正确
  • 错误

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

  • 正确
  • 错误

9. 若数组中有重复元素,去重操作会导致程序输出偏小。( ){{ select(9) }}

  • 正确
  • 错误

选择题

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

  • 6
  • 7
  • 8
  • 9

11. 该程序中的 j 指针在循环中( )。{{ select(11) }}

  • 只增不减
  • 只减不增
  • 有时增加有时减少
  • 保持不变

12. 若将第14行的 ans += i - j; 改为 ans += 1;,程序的功能变为( )。{{ select(12) }}

  • 统计满足条件的区间个数
  • 统计满足条件的左端点个数
  • 统计数组长度
  • 统计去重后的元素个数

程序三:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

char s1[5005], s2[5005];
int dp[5005];

int main() {
    scanf("%s%s", s1 + 1, s2 + 1);
    int n = strlen(s1 + 1);
    int m = strlen(s2 + 1);
    for (int i = 1; i <= n; ++i) {
        for (int j = m; j >= 1; --j) {
            if (s1[i] == s2[j]) {
                dp[j] = max(dp[j], dp[j - 1] + 1);
            }
        }
    }
    int ans = 0;
    for (int j = 1; j <= m; ++j) ans = max(ans, dp[j]);
    printf("%d\n", ans);
    return 0;
}

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

13. 该程序的功能是求两个字符串的最长公共子序列长度。( ){{ select(13) }}

  • 正确
  • 错误

14. 若输入 s1 = "abcde",s2 = "ace",程序的输出为 3。( ){{ select(14) }}

  • 正确
  • 错误

15. 该程序使用了一维数组 dp 进行空间优化,与二维 DP 输出结果相同。( ){{ select(15) }}

  • 正确
  • 错误

选择题

16. 若输入 s1 = "abab",s2 = "baba",程序的输出为( )。{{ select(16) }}

  • 1
  • 2
  • 3
  • 4

17. 该程序中内层循环 for (int j = m; j >= 1; j--) 采用倒序的原因是( )。{{ select(17) }}

  • 防止 dp[j-1] 被覆盖
  • 提高程序运行速度
  • 使代码更简洁
  • 避免数组越界

18. 若将第14行的 dp[j] = max(dp[j], dp[j - 1] + 1) 改为 dp[j] = dp[j - 1] + 1,程序的功能变为( )。{{ select(18) }}

  • 求两个字符串的最长公共子序列
  • 求两个字符串的最长公共子串
  • 求两个字符串的最长公共前缀
  • 求两个字符串的编辑距离