總結:1037 - CSP 2021 提高階第一輪

辜铜星發表於2024-09-15

我的提交記錄與結果

一、 單項選擇題

  1. 以比較為基本運算,對於 \(2n\) 個數,同時找到最大值和最小值,最壞情況下需要的最小的比較次數為( )。
    \(\texttt A\). 4n-2
    \(\texttt B\). 3n+1
    \(\color{#5eb95e}\texttt{C}\). 3n-2
    \(\color{#e74c3c}\texttt D\). 2n+1

    【解析】:

    首先先將原陣列兩兩分組。每組求一個最大,一個最小。使用 \(n\) 次。

    這樣化成 \(n\) 組最大和最小。此時直接最大與最大進行比較,最小與最小進行比較,使用 \(2n-2\) 次。

    總共有 \(n+2n-2=3n-2\) 次。

    【反思】:

    思維不夠。

  2. G 是一個非連通簡單無向圖(沒有自環和重邊),共有 36 條邊,則該圖至少有( )個點。
    \(\texttt A\). 8
    \(\color{#e74c3c}\texttt B\). 9
    \(\color{#5eb95e}\texttt C\). 10
    \(\texttt D\). 11

    【反思】:

    審題不認真:

    G 是一個非連通簡單無向圖(沒有自環和重邊),共有 36 條邊,則該圖至少有( )個點。

  3. 前序遍歷和中序遍歷相同的二叉樹為且僅為( )。
    \(\texttt A\). 只有 1 個點的二叉樹
    \(\color{#e74c3c}\texttt B\). 根結點沒有左子樹的二叉樹
    \(\texttt C\). 非葉子結點只有左子樹的二叉樹
    \(\color{#5eb95e}\texttt D\). 非葉子結點只有右子樹的二叉樹

    【反思】:

    不要只考慮根的情況,在遍歷中,所有部分都是根。

二、閱讀程式

(一)

#include <iostream>
02 #include <cmath>
03 using namespace std;
04
05 const double r = acos(0.5);
06
07 int a1, b1, c1, d1;
08 int a2, b2, c2, d2;
09 
10 inline int sq(const int x) { return x * x; }
11 inline int cu(const int x) { return x * x * x; }
12 
13 int main()
14 {
15 	cout.flags(ios::fixed);
16 	cout.precision(4);
17	
18 	cin >> a1 >> b1 >> c1 >> d1;
19 	cin >> a2 >> b2 >> c2 >> d2;
20	
21 	int t = sq(a1 - a2) + sq(b1 - b2) + sq(c1 - c2);
22	
23 	if (t <= sq(d2 - d1)) cout << cu(min(d1, d2)) * r * 4;
24 	else if (t >= sq(d2 + d1)) cout << 0;
25 	else {
26 		double x = d1 - (sq(d1) - sq(d2) + t) / sqrt(t) / 2;
27 		double y = d2 - (sq(d2) - sq(d1) + t) / sqrt(t) / 2;
28 		cout << (x * x * (3 * d1 - x) + y * y * (3 * d2 - y)) * r;
29 	}
30 	cout << endl;
31 	return 0;
32 }

相關文章