Learn C++ from a baby! 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
#include <iostream>
using namespace std;
struct point {
int x, y, id;
};
bool equals(point a, point b) {
return a.x == b.x && a.y == b.y;
}
bool cmp(point a, point b) { //相当于a<b
return ①;
}
void sort(point A[], int n) { //将关键点的集合按照从小到大方式排序
for ( int i=0; i<n; i++)
for (int j=1; j<n; j++)
if (cmp(A[j], A[j=1])) {
point t = A[j];
A[j] = A[j-1];
A[j-1] = t;
}
}
int unique(point A[], int n) { //unique为去重
int t = 0;
for (int i=0; i<n; i++)
if (②)
A[t++] = A[i];
return t;
}
bool binary_search(point A[], int n, int x, int y) { //二分搜索,判断p是否在A[0]-A[n-1]中
point p;
p.x = x;
p.y = y;
p.id = n;
int a = 0, b = n - 1;
while (a<b) {
int mid = ③;
if (④)
a = mid + 1;
else
b = mid;
}
return equals(A[a], p);
}
const int MAXN = 1000;
point A[MAXN];
int main() {
int n;
cin >> n;
for (int i=0; i<n; i++) {
cin >> A[i].x >> A[i].y;
A[i].id = i;
}
sort(A, n);
n = unique(A,n);
int ans = 0;
for (int i=0; i<n; i++)
for (int j=0; i<n; j++)
if (⑤ && binary_search(A, n, A[i].x, A[j].y) && binary_search(A, n, A[j].x, A[i].y)) {
ans ++;
}
cout << ans << endl;
return 0;
}
39. ① should be filled in ( )
A.a.x != b.x ? a.x < b.x : a.id < b.id
B.a.x != b.x ? a.x < b.x : a.y < b.y
C.equals(a, b) ? a.id < b.id : a.x < b.x
D.equals(a, b) ? a.id < b.id : (a.x != b.x ? a.x < b.x : a.y < b.y)
[Answer]: B
【Analysis】
Because equals compares coordinates, and cmp can only compare coordinates, all options with ids cannot be selected, so you can only select B. The priority is to compare x first and then y.
40. ② should be filled in ( )
A.i == 0 || cmp(A[i], A[i-1])
B.t == 0 || equals(A[i], A[t-1])
C.i == 0 || !cmp(A[i], A[i-1])
D.t == 0 || !equals(A[i], A[t-1])
[Answer]: D
【Analysis】
When A[t-1] is not equal to A[i], put A[i] at the position of A[t], and then t++. Choose D
41. ③ should be filled in ( )
A.b - (b - a) / 2 +1
B.(a + b + 1) >> 1
C.(a + b) >> 1
D.a + (b - a + 1) / 2
[Answer]: C
【Analysis】
There are many binary search methods. There is no need to memorize them. The only criterion is that there cannot be an infinite loop. The infinite loop occurs when a and b are closest (the difference between a and b is 1), that is, the interval is [a, a+ 1]. At this time, it needs to be split into two intervals [a] and [a+1], and the intervals split into by binary search are [a, mid], [mid+1, a+1], then it is necessary to infer that the value of mid equal to a. You can substitute b=a+1, choose C. Shifting one position to the right is equivalent to dividing by 2.
42. ④ should be filled in ( )
A.!cmp(A[mid], p)
B.cmp(A[mid], p)
C.cmp(p, A[mid])
D.!cmp(p, A[mid])
[Answer]: B
【Analysis】
The purpose is to eliminate the part that is not equal to p, 4 options. Only when option B is true, A[0]...A[mid] cannot be equal to p, because here is A[0]...A[mid ] is less than p
43. ⑤ should be filled in ( )
A.A[i].x == A[j].x
B.A[i].id < A[j].id
C.A[i].x == A[j].x && A[i].id < A[j].id
D.A[i].x < A[j].x && A[i].y < A[j].y
[Answer]: D
【Analysis】
A[i] should be at the lower left of A[j]. The goal of the program is to find a point whose coordinates are equal to A[i].x and A[j].y between point i and point j through binary search.