2024 年第 19 题阅读程序枚举与模拟普及-
程序(一):输入50时 sumPrimes(50) 的输出
题目
阅读下面的程序,回答问题。 #include <iostream> using namespace std; 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; } int countPrimes(int n) { int count = 0; for (int i = 2; i <= n; i++) { if (isPrime(i)) { count++; } } return count; } int sumPrimes(int n) { int sum = 0; for (int i = 2; i <= n; i++) { if (isPrime(i)) { sum += i; } } return sum; } int main() { int x; cin >> x; cout << countPrimes(x) << " " << sumPrimes(x) << endl; return 0; } 当输入为50时,sumPrimes(50) 的输出为( )
考点拆解
搜索算法(DFS/BFS)
易错提醒
阅读程序题要按变量变化顺序手推,不要跳步
选择题要检查单位、边界和题目中的否定词