#rscK004. RSC 2025初中组样题
RSC 2025初中组样题
RSC 2025初中组样题
一、单项选择题
- 指针声明正确的是:{{ select(1) }}
- int p = &a;
- int* p = a;
- int* p = &a;
- int p* = &a;
- 动态分配的数组应如何释放? {{ select(2) }}
- delete arr;
- delete arr[];
- delete[] arr;
- delete (arr);
- 函数重载的必要条件是:{{ select(3) }}
- 返回值类型不同
- 参数列表不同
- 函数名不同
- 作用域不同
- 递归函数必须包含:{{ select(4) }}
- 循环语句
- 静态变量
- 终止条件
- 全局变量
- 读取文件应使用哪个类?{{ select(5) }}
- ofstream
- ifstream
- fstream
- iostream
- 结构体变量初始化正确的是:{{ select(6) }}
- Student s = "Tom", 90;
- Student s = {"Tom", 90};
- Student s = (name: "Tom", score: 90);
- Student s = new Student{"Tom", 90};
7.链表节点 Node{2, nullptr} 的 next 指针指向:{{ select(7) }}
- 自身
- 空地址
- 下一个节点
- 头节点
- 二维数组 matrix[3][3] 中,matrix[1][2] 的值是:{{ select(8) }}
- 5
- 6
- 8
- 未知
- s.substr(0,5) 的作用是: {{ select(9) }}
- 删除前5个字符
- 截取前5个字符
- 替换前5个字符
- 反转前5个字符
- 质数判断优化时,循环条件应为: {{ select(10) }}
- i < n
- i <= sqrt(n)
- i*i <= n
- i <= n/2
- 埃拉托斯特尼筛法用于:
int ai[1000001]={};
int n;
cin>>n;
for(int i=2;i*i<=n;i++){
if(ai[i]==0){
for(int j=i*i;j<=n;j+=i){
ai[j]=1;
}
}
}
{{ select(11) }}
- 计算质因数
- 筛选质数
- 求最大公约数
- 分解整数
- 辗转相除法求GCD(12,18)的结果是:
int gcd(int a,int b){
if(a%b==0){
return b;
}
return gcd(b,a%b);
}
{{ select(12) }}
- 3
- 6
- 12
- 18
- 快速幂算法的时间复杂度是:
//递归计算a的b次方
int fast_pow(int a,int b){
if(b==0){ //边界条件
return 1;
}
//重复过程
if(b%2==1){
return a*fast_pow(a*a,b/2);
}else{
return fast_pow(a*a,b/2);
}
}
{{ select(13) }}
- O(n)
- O(log n)
- O(n²)
- O(1)
- 分解质因数时,循环变量
i的起始值是: {{ select(14) }}
- 0
- 1
- 2
- 任意数
- 数字反转函数 reverse(123) (自定义)的返回值可能是: {{ select(15) }}
- 123
- 321
- 213
- 312
- 二分查找的前提条件是: {{ select(16) }}
- 数组无序
- 数组有序
- 数组元素唯一
- 数组长度为奇数
- 贪心算法解决硬币找零问题时:
硬币找零问题:给定一组硬币面额(如 1元、5元、10元、25元等)和一个需要找零的目标金额,要求用最少数量的硬币凑出该金额。
{{ select(17) }}
- 总是选择面值最小的硬币
- 总是选择面值最大的硬币
- 随机选择硬币
- 需要回溯
-
递归计算 factorial(5) 的结果是:
int factorial(int n) { if (n <= 1) { return 1; } return n * factorial(n - 1); }
{{ select(18) }}
- 5
- 24
- 120
- 720
19.快速幂算法中,b 的更新操作是: {{ select(19) }}
- b *= 2
- b /= 2
- b -= 1
- b += 1
- 设硬币由10,5,1面值,若 money=27,即找27的零,则贪心算法找零需要的硬币数是:
硬币找零问题:给定一组硬币面额(如 1元、5元、10元、25元等)和一个需要找零的目标金额,要求用最少数量的硬币凑出该金额。
{{ select(20) }}
- 5(10*2 + 5*1 + 1*2)
- 3(10+10+5+2)
- 4(10+5+5+5+2)
- 5(5+5+5+5+7)
二、阅读程序题
程序阅读1
阅读以下 C++ 代码,该代码实现了一些数论相关的功能以及使用了动态内存分配和函数重载。请根据代码回答后面的问题。
#include <iostream>
#include <cmath>
bool isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i < n; i++) {
if (n % i == 0) return false;
}
return true;
}
bool isPrime(double num) {
int n = static_cast<int>(num);
if (n <= 1) return false;
for (int i = 2; i <= std::sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}
// 动态分配数组并填充质数
int* generatePrimes(int size) {
int* primes = new int[size];
int count = 0;
int num = 2;
while (count < size) {
if (isPrime(num)) {
primes[count++] = num;
}
num++;
}
return primes;
}
int main() {
int size = 10;
int* primeArray = generatePrimes(size);
std::cout << "前 " << size << " 个质数为: ";
for (int i = 0; i < size; i++) {
std::cout << primeArray[i] << " ";
}
std::cout << std::endl;
// 检查一个浮点数是否为质数
double testNum = 17.0;
if (isPrime(testNum)) {
std::cout << testNum << " 被认为是质数。" << std::endl;
} else {
std::cout << testNum << " 不被认为是质数。" << std::endl;
}
delete[] primeArray;
return 0;
}
- 代码中定义了两个 isPrime 函数,这体现了 C++ 的什么特性?( ){{ select(21) }}
- 函数递归
- 函数重载
- 动态内存分配
- 模板函数
- isPrime(double num) 函数中,static_cast<int>(num) 的作用是什么?( ){{ select(22) }}
- 将浮点数 num 转换为整数,用于后续质数判断
- 将整数 num 转换为浮点数
- 只是一个占位操作,无实际作用
- 用于检查 num 是否为整数
- 在 generatePrimes 函数中,while (count < size) 循环的作用是什么?( ){{ select(23) }}
- 计算小于 size 的所有数
- 找到 size 个质数并填充到动态分配的数组中
- 检查 size 是否为质数
- 动态分配 size 大小的数组
- 假设 size 的值为 5,generatePrimes 函数返回的数组中存储的质数是哪些?( ){{ select(24) }}
- 2, 3, 5, 7, 9
- 2, 3, 5, 7, 11
- 1, 2, 3, 5, 7
- 2, 4, 6, 8, 10
- 在 main 函数中,delete[] primeArray; 这行代码的作用是什么?( ){{ select(25) }}
- 删除 primeArray 指针
- 释放 primeArray 所指向的动态分配的内存,避免内存泄漏
- 将 primeArray 所指向的数组元素全部设为 0
- 只是一个语法习惯,无实际作用
- 关于代码中对浮点数 testNum 的质数判断,以下说法正确的是?( ){{ select(26) }}
- 由于浮点数精度问题,这种判断可能不准确
- 先将浮点数转换为整数再判断,所以结果准确
- 浮点数不能进行质数判断,代码会报错
- 直接对浮点数进行质数判断,结果不可靠
程序阅读2
阅读以下代码段,回答第1~6题:
#include <iostream>
using namespace std;
// 代码段A
bool isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) return false;
}
return true;
}
// 代码段B
struct Node {
int data;
Node* next;
};
void insertFront(Node* head, int val) {
Node* newNode = new Node{val, head->next};
head->next = newNode;
}
// 代码段C
int mystery(int n) {
if (n == 0) return 1;
return n * mystery(n - 1);
}
// 代码段D
int fastPow(int a, int b) {
int res = 1;
while (b > 0) {
if (b % 2 == 1) res *= a;
a *= a;
b /= 2;
}
return res;
}
int main() {
// 测试A
cout << "Is 17 prime? " << boolalpha << isPrime(17) << endl;
// 测试B
Node* head = new Node{0, nullptr};
insertFront(head, 1);
insertFront(head, 2);
insertFront(head, 3);
cout << "Linked list: ";
Node* current = head->next;
while (current != nullptr) {
cout << current->data << " ";
current = current->next;
}
cout << endl;
// 测试C
cout << "5! = " << mystery(5) << endl;
// 测试D
cout << "2^10 = " << fastPow(2, 10) << endl;
// 释放链表内存
while (head != nullptr) {
Node* temp = head;
head = head->next;
delete temp;
}
return 0;
}
- 代码段A中,当输入n=25时,循环执行次数是 {{ select(27) }}
- 3次
- 4次
- 5次
- 6次
2.代码段B的insertFront函数执行后,链表会发生什么变化? {{ select(28) }}
- 在头节点前插入新节点
- 在头节点后插入新节点
- 替换头节点的数据
- 链表变为环形结构
- 调用mystery(4)的返回值是 {{ select(29) }}
- 4
- 12
- 24
- 48
- 代码段D中,fastPow(2, 5)的计算结果是 {{ select(30) }}
- 16
- 25
- 32
- 64
- 若删除代码段A中的i * i <= n,改为i <= n,会导致 {{ select(31) }}
- 正确判断所有质数
- 将合数误判为质数
- 栈溢出错误
- 时间复杂度从O(√n)变为O(n)
- 在代码段B的基础上,若连续执行:
insertFront(head, 5);
insertFront(head, 7);
最终链表的顺序是 {{ select(32) }}
- 5 → 7
- 7 → 5
- 头节点 → 5 → 7
- 头节点 → 7 → 5
程序阅读3
假设你有一个文本文件 numbers.txt,文件中每行存储一个整数。现在要求编写一个 C++ 程序,通过分治递归的方法,计算文件中所有整数的总和,并将结果输出到另一个文件 result.txt 中。
#include <iostream>
#include <fstream>
#include <vector>
void readNumbersFromFile(const std::string& filename,std::vector<int>& numbers) {
std::ifstream file(filename);
if (file.is_open()) {
int num;
while (file >> num) {
numbers.push_back(num);
}
file.close();
} else {
std::cerr << "无法打开文件: " << filename << std::endl;
}
}
int divideAndConquerSum(const std::vector<int>& numbers, int left, int right) {
if (left > right) {
return 0;
}
if (left == right) {
return numbers[left];
}
int mid = (left + right) / 2;
int leftSum = divideAndConquerSum(numbers, left, mid);
int rightSum = divideAndConquerSum(numbers, mid + 1, right);
return leftSum + rightSum;
}
int main() {
std::vector<int> numbers;
readNumbersFromFile("numbers.txt", numbers);
int totalSum = divideAndConquerSum(numbers, 0, numbers.size() - 1);
std::ofstream outFile("result.txt");
if (outFile.is_open()) {
outFile << "文件中所有整数的总和为: " << totalSum << std::endl;
outFile.close();
} else {
std::cerr << "无法打开文件: result.txt" << std::endl;
}
return 0;
}
- 在 readNumbersFromFile 函数中,while (file >> num) 的作用是什么?( ){{ select(33) }}
- 从文件中读取一个字符
- 从文件中读取一行字符串
- 从文件中读取一个整数,并存储到 num 中
- 检查文件是否读取到末尾
- divideAndConquerSum 函数体现了分治递归的思想,其中 int mid = (left + right) / 2; 这行代码的作用是?( ){{ select(34) }}
- 计算数组的中间位置,用于将数组分成两部分
- 计算数组元素的平均值
- 作为递归的终止条件
- 用于判断数组是否为空
- 如果 numbers.txt 文件中的内容为 1 2 3 4 5,那么 divideAndConquerSum 函数递归调用的次数是多少?( ){{ select(35) }}
- 4
- 5
- 8
- 9
- 在 main 函数中,如果 readNumbersFromFile 函数无法打开 numbers.txt 文件,程序会如何处理?( ){{ select(36) }}
- 程序会继续执行,totalSum 会被赋值为 0
- 程序会输出错误信息并终止
- 程序会尝试创建一个新的 numbers.txt 文件
- 程序会忽略该错误,继续执行后续代码
- 关于 divideAndConquerSum 函数的递归终止条件,以下说法正确的是?( ){{ select(37) }}
- 当 left > right 时,说明数组为空,返回 0
- 当 left == right 时,说明只有一个元素,返回该元素值
- 当 mid 为 0 时,返回 0
- A 和 B 都正确
- 如果要在程序中添加功能,统计文件中奇数的个数,并将结果也输出到 result.txt 文件中,以下代码应插入到程序的哪个位置最合适?( ){{ select(38) }}
- 在 readNumbersFromFile 函数中读取数字后统计
- 在 divideAndConquerSum 函数中递归过程中统计
- 在 main 函数中计算出 totalSum 之后统计
- 在 main 函数中打开 result.txt 文件之后统计
三、完善程序题
【密码强度评估程序】
一般来说,质数在密码学中有重要应用,因为基于质数的运算往往具有单向性,增加破解难度。如果密码对应的数值是质数,由于其只有1和自身两个因数,在某些加密场景下会更难被暴力破解,所以我们认为是质数的密码数值在这方面有一定强度优势。同时,质因数分解也与密码强度相关,质因数数量越多,意味着密码数值的分解方式越复杂,暴力破解时需要尝试的组合更多,密码强度也就越高。另外,通过检查密码中是否包含特定数字组合,能从另一个角度评估密码安全性,若包含特殊数字组合,可能会被攻击者利用,降低密码强度。以下是该程序的部分代码,请你完善它。
#include <iostream>
#include <vector>
#include <cmath>
// 判断一个数是否为质数
bool isPrime(int num) {
if (num <= 1) return false;
for (int i = 2; i * i <= num; ++i) {
if (____________________) {
return false;
}
}
return true;
}
// 找出一个数的所有因数
std::vector<int> findFactors(int num) {
std::vector<int> factors;
for (int i = 1; i <= num; ++i) {
if (num % i == 0) {
____________________;
}
}
return factors;
}
// 分解质因数
std::vector<int> primeFactorization(int num) {
std::vector<int> primeFactors;
for (int i = 2; i <= num; ++i) {
while (____________________) {
primeFactors.push_back(i);
num /= i;
}
}
return primeFactors;
}
// 根据质因数数量评估密码强度等级
int evaluatePasswordStrength(const std::vector<int>& primeFactors) {
int strength = 0;
// 假设质因数数量越多,密码强度越高
strength = primeFactors.size();
if (strength < 3) {
return 1; // 低强度
} else if (strength < 5) {
return 2; // 中强度
} else {
return 3; // 高强度
}
}
// 检查密码是否包含特定数字组合(模拟实际安全策略)
bool checkSpecialCombination(int password, int specialNumber) {
std::string passwordStr = std::to_string(password);
std::string specialStr = std::to_string(specialNumber);
return ____________________;
}
// 使用快速幂算法计算密码数值的幂次方(新增知识点)
int fastPower(int base, int exponent) {
int result = 1;
while (exponent > 0) {
if (exponent % 2 == 1) {
result = result * base;
}
base = base * base;
exponent = exponent / 2;
}
return result;
}
// 利用函数重载,实现不同数据类型的快速幂运算(新增知识点)
double fastPower(double base, int exponent) {
double result = 1.0;
while (exponent > 0) {
if (exponent % 2 == 1) {
result = result * base;
}
base = base * base;
exponent = exponent / 2;
}
return result;
}
int main() {
int password = 123456;
std::cout << "密码数值: " << password << std::endl;
std::cout << "该密码数值是否为质数: ";
if (____________________) {
std::cout << "是" << std::endl;
} else {
std::cout << "否" << std::endl;
}
std::vector<int> factors = findFactors(password);
std::cout << "该密码数值的因数有: ";
for (int factor : factors) {
std::cout << factor << " ";
}
std::cout << std::endl;
std::vector<int> primeFactors = primeFactorization(password);
std::cout << "该密码数值的质因数分解为: ";
for (int primeFactor : primeFactors) {
std::cout << primeFactor << " ";
}
std::cout << std::endl;
int strength = evaluatePasswordStrength(primeFactors);
std::cout << "根据质因数数量评估的密码强度等级: " << strength << std::endl;
int specialNumber = 34;
if (checkSpecialCombination(password, specialNumber)) {
std::cout<<"密码包含特殊数字组合"<<specialNumber<<",可能存在风险"<< std::endl;
} else {
std::cout << "密码不包含特殊数字组合 " << specialNumber << std::endl;
}
// 计算密码数值的5次幂
int powerResult = fastPower(password, 5);
std::cout << "密码数值的5次幂为: " << powerResult << std::endl;
// 计算3.5的4次幂(测试函数重载)
double doublePowerResult = fastPower(3.5, 4);
std::cout << "3.5的4次幂为: " << doublePowerResult << std::endl;
return 0;
}
请把你的答案填入以下对应空位中(不要输入多余空格)。
填空1:{{ input(39) }}
填空2:{{ input(40) }}
填空3:{{ input(41) }}
填空4:{{ input(42) }}
填空5:{{ input(43) }}