Learn C++ from a young age! Record the questions in the CSP-J exam preparation study process and record every moment.
Attached is a summary post: Analysis of past CSP-J preliminary test questions | Summary_csp past past questions_Blog of a communicator who loves programming-CSDN blog
(Prime factorization) Given a positive integer n, please output the result of decomposing n into prime factors. The results are output from small to large.
For example: input n=120, the program should output 2 2 2 3 5, which means 120=2×2×2×3×5. The input guarantee is 2≤n≤10^9. Tip: First enumerate the variable i from small to large, and then use i to divide n continuously to find all prime factors.
Try the completion program.
#include <cstdio>
using namespace std;
int n, i;
int main() {
scanf("%d", &n);
for (i = ①; ② < = n; i ++) {
③ {
printf("%d ", i);
n = n / i;
}
}
if(④)
printf("%d ", ⑤);
return 0;
}
34. ① should be filled in ( )
A.1
B.n-1
C.2
D.0
[Answer]: C
【Analysis】
For each factor starting from 2, use it to divide n, choose C
35. ② should be filled in ( )
A.n / i
B.n / (i * i)
C.i * i
D.i * i * i
[Answer]: B
【Analysis】
Prime factorization only tries to divide those prime factors less than or equal to √n, choose B
36. ③ should be filled in ( )
A.if (n % i == 0)
B.if (i * i <= n)
C.while (n % i == 0)
D.while (i * i <= n)
【Answer】:
【Analysis】
If n%i==0, divide repeatedly to obtain multiple prime factors, choose C
37. ④ should be filled in ( )
A.n > 1
B.n <= 1
C.i < n / i
D.i + i <= n
【Answer】:A
【Analysis】
After trying to divide all factors less than or equal to √n, n is still left (it has not been divided into 1), which means that the value of n at this time is the only prime factor greater than √n.
38. ⑤ should be filled in ( )
A.2
B.n / i
C.n
D.i
[Answer]: C
【Analysis】
Refer to question 4, n should be output at this time. For example, when n=5, output 5