選擇題部分
在 C++ 中,以下代表布爾類型的是( )
- 選項:A. double B. bool C. int D. char
- 答案:B
- 解析:C++ 中布爾類型的關鍵字為
bool
,用于存儲邏輯值true
或false
。
執行以下程序,輸出的結果是( )
int x = 1, y = 2;
int z = (7 * x + 11 * y) - 11;
cout << z << endl;
- 選項:A. 12 B. 13 C. 18 D. 20
- 答案:C
- 解析:計算順序為
7×1=7
,11×2=22
,相加得 29 后減 11,結果為 18。
執行以下程序,輸出的結果是( )
int a = 28, b = 10;
if (a <= 10) { b = 5; }
else if (a <= 20) { b = 15; }
else { b = 25; }
cout << b << endl;
- 選項:A. 0 B. 5 C. 15 D. 25
- 答案:D
- 解析:
a=28
不滿足前兩個條件,執行else
分支,b
賦值為 25。
下列符號中表示邏輯運算符 "或者" 的是( )
- 選項:A. & B. && C. | D. ||
- 答案:D
- 解析:C++ 中邏輯或運算符為
||
,&&
為邏輯與,&
和|
為按位運算符。
執行以下代碼,程序輸出的結果是( )
int cnt = 0;
for (int i = 0; i <= 2; i++) { for (int j = 0; j <= 4; j++) { cnt += i * j; }
}
- 選項:A. 18 B. 16 C. 24 D. 30
- 答案:D
- 解析:
i=1
時累加和為 10,i=2
時為 20,總和為 30(i=0
時貢獻 0)。
編程題部分
題目 1:計算樹苗總數
項目 | 描述 |
---|
題目描述 | 已知每班樹苗數m 和班級數n ,求總樹苗數。 |
輸入 | 一行兩個正整數m 、n 。 |
輸出 | 一行,m*n 的結果。 |
樣例 | 輸入6 10 ?→ 輸出60 。 |
#include <iostream>
using namespace std;int main() {int m, n;cin >> m >> n;cout << m * n;return 0;
}
題目 2:分類求和(A 類與 B 類數)
項目 | 描述 |
---|
題目描述 | 將 1 到n 的數分為能被t 整除的 A 類和不能的 B 類,求兩類和。 |
輸入 | 一行兩個正整數n 、t 。 |
輸出 | 一行,A 類和與 B 類和(空格分隔)。 |
樣例 | 輸入20 7 ?→ 輸出21 189 。 |
#include <iostream>
using namespace std;int main() {int n, t;cin >> n >> t;int k = n / t;int sum_A = t * k * (k + 1) / 2;int total_sum = n * (n + 1) / 2;int sum_B = total_sum - sum_A;cout << sum_A << " " << sum_B;return 0;
}
題目 3:計算月份天數(含閏年判斷)
項目 | 描述 |
---|
題目描述 | 計算指定年份y 和月份m 的天數,閏年 2 月為 29 天。 |
輸入 | 一行兩個正整數y 、m 。 |
輸出 | 一行,該月的天數。 |
樣例 | 輸入2024 2 →輸出29 ;輸入2018 3 →輸出31A |
#include <iostream>
using namespace std;int main() {int y, m;cin >> y >> m;int days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};bool isLeap = (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);if (isLeap && m == 2) cout << 29;else cout << days[m];return 0;
}
題目 4:數字反轉(去除前導零)
項目 | 描述 |
---|
題目描述 | 將數字反轉并去除前導零(如120→21 )。 |
輸入 | 第一行n ,第二行n 個正整數。 |
輸出 | 一行n 個反轉后的整數(空格分隔)。 |
樣例 | 輸入3 和123 320 78 ?→ 輸出321 23 87 。 |
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;int main() {int n;cin >> n;for (int i = 0; i < n; i++) {string s;cin >> s;reverse(s.begin(), s.end());int j = 0;while (j < s.length() && s[j] == '0') j++;if (j == s.length()) cout << 0;else cout << s.substr(j);if (i < n - 1) cout << " ";}return 0;
}
題目 5:小球顏色收集(滑動窗口)
項目 | 描述 |
---|
題目描述 | 在n 個小球中選連續k 個,求最多不同顏色數。 |
輸入 | 第一行n 和k ,第二行n 個顏色值。 |
輸出 | 一行,最大不同顏色數。 |
樣例 | 輸入7 3 和1 2 1 2 3 3 1 →輸出3 。 |
#include <iostream>
#include <vector>
using namespace std;int main() {int n, k;cin >> n >> k;vector<int> c(n);for (int i = 0; i < n; i++) cin >> c[i];vector<int> count(300001, 0);int unique = 0, max_colors = 0;for (int i = 0; i < k; i++) {if (count[c[i]] == 0) unique++;count[c[i]]++;}max_colors = unique;for (int i = k; i < n; i++) {count[c[i - k]]--;if (count[c[i - k]] == 0) unique--;if (count[c[i]] == 0) unique++;count[c[i]]++;if (unique > max_colors) max_colors = unique;}cout << max_colors << endl;return 0;
}
所有題目總結
題目類型 | 核心知識點 | 關鍵算法 / 數據結構 |
---|
樹苗總數 | 基礎算術 | 直接乘法 |
分類求和 | 等差數列求和、數學推導 | 公式計算 |
月份天數 | 閏年判斷邏輯 | 條件分支 |
數字反轉 | 字符串處理、前導零處理 | 字符串反轉、遍歷 |
小球顏色收集 | 滑動窗口、區間統計 | 計數數組、動態維護 |