二、阅读程序(程序输入不超过数组或字符串定义的范围;判断题正确填 √,错误填 ⨉ ;除特殊说明外,判断题 1.5 分,选择题 3 分,共计 40 分)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | #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; } |
0 of 5 Questions completed
Questions:
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading…
You must sign in or sign up to start the quiz.
You must first complete the following:
0 of 5 Questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 point(s), (0)
Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)
2、若将 isPrime(i)
函数中的条件改为 i<=n/2
,输入 20时, countPrimes(20)
的输出将变为 6。()
3、sumPrimes
函数计算的是从 2 到 n之间的所有素数之和。
4、当输入为 50时,sumPrimes(50)
的输出为( )。
5、如果将 for (int i = 2; i * i <= n; i++)
改为 for (int i = 2; i <= n; i++)
,输入 10 时,程序的输出( )。