Learn C++ from a baby! Record the questions in the process of CSP-J preparation and study, and record every moment.
Attached is a summary post: Analysis of the real questions of the CSP-J preliminary competition over the years | Summary
Print all positive factors of positive integer n from small to large.
Try to complete the enumeration procedure.
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int>fac;
fac.reserve((int)ceil(sqrt(n))); //为fac申请一片内存空间,对程序理解无影响
int i;
for (i=1; i*i<n; ++i) { //此循环是将小于√n的所有因数放入到fac中
if (①) {
fac.push_back(i);
}
}
for (int k=0; k<fac.size(); ++k) { //将fac中已经存放的数据打印出来
cout << ② << "";
}
if (③) { //如果不能理解可以先跳过3、4直接看第5个空。这里用来处理i*i=n的数
cout << ④ << "";
}
for (int k=fac.size()-1; k>=0; --k) { //逆序枚举fac
cout << ⑤ << "";
}
}
35. Place ① should be filled in ( )
A.n % i == 0
B.n % i == 1
C.n % (i-1) == 0
D.n % (i-1) == 1
[Answer]: A
【Analysis】
The topic is to find the factor of n, so when it is a factor of n here, put it in fac. So choose A
36. Place ② should be filled in ( )
Year / year[k]
B.fac[k]
C.fac[k]-1
Dn / (fac[k]-1)
[Answer]: B
【Analysis】
Print out the factor of the small fish √n that has been put into fac
37. Place ③ should be filled in ( )
A.(i-1) * (i-1) == n
B.(i-1) * i == n
C.i * i == n
D.i * (i-1) == n
[Answer]: C
【Analysis】
If n=9, the output before line 16 is 1, and the output from line 24 to 26 is 9, and there is still a 3 missing, so the combination of 3 and 4 should be able to output 3. Because 3*3=9, so roll it backwards and choose C
38. Place ④ should be filled in ( )
A.n-i
B.n-i+1
C.i-1
D.i
[Answer]: D
【Analysis】
Same as question 37, output this i, that is, √n
39, ⑤ should be filled in ( )
Year / year[k]
B.fac[k]
C.fac[k]-1
Dn / (fac[k]-1)
[Answer]: A
【Analysis】
If n=6, its positive factors are 1, 2, 3, 6, 1 and 2 are stored in fac before line 16, and 3 and 6 can be obtained by n/fac[k], so choose A