作者xavier13540 (柊 四千)
看板NTU-Exam
標題[試題] 114-1 黃上恩 基礎程式設計(C++) 期末考
時間Tue Dec 30 03:47:42 2025
課程名稱︰基礎程式設計(C++)
課程性質︰AI學程聯盟選修
課程教師︰温宏斌
考試日期(年月日)︰2025/12/15
考試時限(分鐘):180
試題 :
Part A
1. int main() {
int x[10], y[10];
int z;
/* ... (some code that initializes x and y) ... */
add(x, y, &z);
return 0;
}
You want to declare the function add so that:
1. It does not return any value.
2. It can modify the value of z inside the function.
3. The call add(x, y, &z); is valid in standard C.
Which of the following function declarations is correct?
(A) int add(int a[10], int b[10], int c);
(B) void add(int *a, int *b, int c);
(C) void add(int a[10], int b[10], int *c);
(D) int* add(int a[10], int b[10], int *c);
2. int main() {
int arr[5] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
cout << n;
return 0;
}
What is the output?
(A) 4
(B) 5
(C) 0
(D) 20
3. ifstream file("data.txt"); // data.txt 內容為: Hello World
string s;
/* [INSERT CODE HERE] */
cout << s;
You want the output to be Hello World (reading the full line with spaces).
Which line of code is correct?
(A) file >> s;
(B) getline(file, s);
(C) file.get(s);
(D) file.read(s);
4. int main() {
int rows = 3;
int cols = 4;
// Allocate a dynamic 2D array
int **matrix = new int*[rows];
for (int i = 0; i < rows; i++) {
matrix[i] = new int[cols];
}
/* ... (code that uses the matrix) ... */
// [INSERT DEALLOCATION CODE HERE]
return 0;
}
You want to write the code to correctly free all the memory allocated for
matrix. The code must ensure that:
1. Every array allocated with new[] is properly deleted with delete[].
2. No memory leaks occur (all rows and the pointer array itself are freed).
3. The order of deallocation is valid (accessing valid memory).
Which of the following function declarations is correct?
(A) delete[] matrix;
(B) delete[] matrix;
for (int i = 0; i < rows; i++) {
delete[] matrix[i];
}
(C) for (int i = 0; i < rows; i++) {
delete[] matrix[i];
}
delete[] matrix;
(D) for (int i = 0; i < rows; i++) {
delete matrix[i];
}
delete matrix;
5. ifstream iff("input.txt");
// Check if input.txt exists or not
if( _________ ){
cout << "input.txt does not exist" << endl;
}
Which of the following is a correct way to check whether opening the file
failed?
(A) !iff
(B) !iff.is_open()
(C) Both (A) and (B) are correct
(D) Neither (A) nor (B) is correct
6. #include <iostream>
using namespace std;
int g = 10;
void foo(int arr[], int x, int& y) {
arr[0] = arr[0] + 5;
x = x + 5;
y = y + 5;
g = g + 5;
}
int main() {
int a[3] = {10, 20, 30};
int b = 10;
int c = 10;
foo(a, b, c);
cout << a[0] << " " << b << " " << c << " " << g << endl;
return 0;
}
What is the output?
(A) 15 10 10 10
(B) 10 10 10 10
(C) 15 10 15 15
(D) 10 15 15 15
7. char s[] = "Hello";
char *p = s;
p[1] = 'A';
*(p + 4) = '!';
std::cout << s << endl;
What is the output?
(A) HAll!
(B) Ael!o
(C) Aell!
(D) Hal!o
8. char s[20] = "Hello";
s[2] = '\0';
std::cout << strlen(s) << endl;
What is the output?
(A) 5
(B) 2
(C) 3
(D) 1
9. #include <iostream>
#include <iomanip>
using namespace std;
int main() {
double val = 3.14159;
cout << fixed << setprecision(2) << val;
return 0;
}
What is the output?
(A) 3.14159
(B) 3.14
(C) 3.1
(D) 3.142
10. int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
cout << matrix[1][0] << endl;
What is the output?
(A) 1
(B) 2
(C) 4
(D) 5
Part B
Instructions:
‧ You may pick an arbitrary number of problems to solve with a
total score of
up to 50%.
‧ Please strictly follow the
input and output specifications described in each
problem. Any deviation that causes test case errors will result in point de-
duections, for which the student is fully responsible.
‧ Name your files by problem number:
Problem 1 → p1.cpp, ...
‧ Zip all program files into one archive named with your student ID (e.g.,
411511004.zip or 411511004.7z). Submit through
NTU Cool. If the system is
slow, other methods will be announced.
‧ You have a 10-minute grace period (until 12:10 PM) to submit.
‧
Submissions after 12:10 PM will not be accepted.
‧ You are allowed to open any notes or books. However, you are
not allowed to
copy others' code AND browse on the internet. Otherwise, you'll get 0% in
this examination and be punished by the school regulations.
‧ Carefully read the statements and requirements of each problem.
Problem 01 (10%)
You are a Pokémon Trainer and you want to write a simple program to determine
whether you can successfully catch a Pokémon.
Each Pokémon has the following attributes:
‧
Name (string)
‧
Rarity (integer, 1-5) — a higher value means the Pokémon is harder to catch
‧
CatchRate (double, e.g., 0.8, 1.2, etc.) — a higher value increases the
chance of catching
You are also given a
ballPower (1-5), representing the strength of the PokéBall
you throw.
A capture is successful if:
‧ catchScore ≧ rarity, and catchScore = ballPower * catchRate
Otherwise, the Pokémon escapes.
Input Format:
‧ The first line contains an integer N, the number of Pokémons.
‧ The next N lines each contains:
<name(string)> <rarity(int)> <catchRate(double)>
‧ The final two lines contain:
<ballPower(int)>
<targetPokemonName(string)>
Output Format:
If the capture succeeds: Caught <name>
If the capture fails: <name> escaped
Constraints -
‧ 1 ≦ N ≦ 100
‧ Pokémon names contain no spaces and appear exactly as given in the input
‧ 1 ≦ rarity ≦ 5
‧ catchRate > 0
‧ 1 ≦ ballPower ≦ 5
Input Format
$n$
$s_1$ $r_1$ $c_1$
$s_2$ $r_2$ $c_2$
$\vdots$
$s_n$ $r_n$ $c_n$
$b$
$t$
‧ $n$ 為一不超過 100 的正整數。
‧ 對任意 $i \in \{1, 2, \ldots, n\}$,均有
— $s_i$ 為一以大小寫英文字母組成且長度不超過 10 的非空字串,
— $r_i$ 為一不超過 5 的正整數,且
— $c_i$ 為一不超過 10 的正雙精度浮點數 (double)。
‧ $b$ 為一不超過 5 的正整數。
‧ $t$ 為一以大小寫英文字母組成且長度不超過 10 的非空字串。
‧ $s_1, s_2, \ldots, s_n$ 兩兩相異,且 $t \in \{s_1, s_2, \ldots, s_n\}$。
Output Format
若捕捉寶可夢 $t$ 成功,輸出
Caught $t$
若捕捉寶可夢 $t$ 失敗,輸出
$t$ escaped
Sample Input 1
3
Pikachu 2 1.5
Bulbasaur 3 2
Charmander 4 0.8
3
Pikachu
Sample Output 1
Caught Pikachu
Sample Input 2
3
Pikachu 2 1.5
Bulbasaur 3 2
Charmander 4 0.8
2
Pikachu
Sample Output 2
Caught Pikachu
Sample Input 3
3
Pikachu 2 1.5
Bulbasaur 3 2
Charmander 4 0.8
1
Pikachu
Sample Output 3
Pikachu escaped
Problem 02 (10%)
You are implementing a simple score processing system for a student database.
Each record in the input describes either:
‧ A student's name
‧ A subject with a list of scores
You are recommended to read each input line using
getline, and then use a
stringstream to analyze its content.
There are two types of records in one line and these two types are in arbitrary
order:
‧ Input format
1. Name Record:
NAME <studentName>
This sets the active student. All following score records belong to this
student until another name record appears.
2. Score Record:
SCORE <subjectName> <score1> <score2> ... <scoreN>
‧ <subjectName> is a single word (no spaces)
‧ <score1> ... <scoreN> are values intended to be integers
‧
Negative integer number is valid
‧ Scores may include invalid tokens (e.g., A+, x, 9a......). Only values
that can successfully be parsed as integers should be counted.
Your task is to compute the
average score for each valid SCORE record and output
the result as a
floating-point number.
‧ Output Format
For each SCORE record, output:
<studentName> <subjectName> <averageScore>
The averageScore must be printed as a floating-point value.
Constrains —
‧ Student names contain no spaces
‧ Subject names contain no spaces
‧ Invalid score tokens must be ignored
請注意我們
規定浮點數輸出保留到小數下 2 位。你可以在檔案開頭加上一行 #include
<iomanip> 並在 main() 開頭加上一行 std::cout << std::fixed << std::setprecision(
2); 來達成。
Input Format
$c_1$ $s_1$ $t_{1, 1}$ $t_{1, 2}$ $\ldots$ $t_{1, n_1}$
$c_2$ $s_2$ $t_{2, 1}$ $t_{2, 2}$ $\ldots$ $t_{2, n_2}$
$\vdots$
$c_m$ $s_m$ $t_{m, 1}$ $t_{m, 2}$ $\ldots$ $t_{m, n_m}$
‧ $m$ 為一不超過 5 的非負整數。
‧ 對任意 $i \in \{1, 2, \ldots, m\}$,均有
— $c_i$ 為 "NAME" 或 "SCORE"(不含引號),
— $s_i$ 為一以大小寫英文字母組成且長度不超過 15 的非空字串,且
— 對任意 $j \in \{1, 2, \ldots, n_i\}$,均有 $t_{i, j}$ 為一長度不超過 8 的
非空字串,且每個字元的 ASCII 值皆介於 33 到 126 之間。
‧ 設 $i \in \{1, 2, \ldots, m\}$。我們有
— 當 $c_i$ 為 "NAME" 時,$t_{i, j}$ 的個數(即 $n_i$)為 0;
— 當 $c_i$ 為 "SCORE" 時,
﹡必定存在某個 $i' < i$ 滿足 $c_{i'}$ 為 "NAME",
﹡$t_{i, j}$ 的個數(即 $n_i$)為一不超過 10 的正整數,且
﹡存在能被 parse 成整數的 $t_{i, j}$。
Output Format
$s^n_1$ $s^s_1$ $f_\text{.2f}(a_1)$
$s^n_2$ $s^s_2$ $f_\text{.2f}(a_2)$
$\vdots$
$s^n_{m'}$ $s^s_{m'}$ $f_\text{.2f}(a_{m'})$
‧ $m'$ 為 "SCORE" 的筆數。
‧ $f_\text{.2f}$ 為取到小數下 2 位的浮點數 formatter(e.g. $f_\text{.2f}(5/3)$
為 "1.67" 而 $f_\text{.2f}(-10)$ 為 "-10.00"(不含引號))。
‧ 對任意 $i \in \{1, 2, \ldots, m'\}$,均有
— $s^n_i$ 為第 $i$ 筆 "SCORE" 的學生姓名,
— $s^s_i$ 為第 $i$ 筆 "SCORE" 的科目名稱,且
— $a_i$ 為第 $i$ 筆 "SCORE" 的平均有效分數。
Sample Input 1
NAME Alice
SCORE Math 90 85 92 88
SCORE English 100 87
Sample Output 1
Alice Math 88.75
Alice English 93.50
Sample Input 2
NAME Bob
SCORE History 70 x 80 error 75
Sample Output 2
Bob History 75.00
Sample Input 3
NAME Carol
SCORE Science 100 A+ 90 85 9a
NAME David
SCORE Music 80 70
Sample Output 3
Carol Science 91.67
David Music 75.00
Problem 03 (10%)
You are given N names (each without spaces), stored in a 2D C-string array
char
names[50][31], and one keyword. Your task is to search for the first name that
contains the keyword as a substring,
ignoring letter case.
Print only the index
(0-based) of
the first matched name. If no name contains
the keyword, print -1.
Input:
‧ The input contains several lines in the following format:
— Line 1:
N — number of names (1 ≦ N ≦ 50)
M — maximum length of each name (1 ≦ M ≦ 30)
— Line 2 ~ Line N+1
<name1> ... <nameN> (no spaces)
— Line N+2
keyword contains no spaces (the length of the keyword ≦ 30)
Output:
‧ If a match is found:
print its index (0-based)
‧ Otherwise:
print -1
Hint:
‧ You can use some
operations (
strstr(), etc.) from
<cstring> and
tolower()
from
<cctype> for case-insensitive comparison.
Input Format
$n$ $m$
$s_0$
$s_1$
$\vdots$
$s_{n-1}$
$t$
‧ $n$ 為一不超過 50 的正整數。
‧ $m$ 為一不超過 30 的正整數。
‧ 對任意 $i \in \{0, 1, \ldots, n-1\}$,均有 $s_i$ 為一以大小寫英文字母組成且長
度不超過 $m$ 的非空字串。
‧ $t$ 為一以大小寫英文字母組成且長度不超過 30 的非空字串。
Output Format
若忽略大小寫後 $t$ 出現在 $s_0, s_1, \ldots, s_{n-1}$ 中,請輸出
$i_0$
代表忽略大小寫後 $t$ 最早出現在 $s_{i_0}$ 中;否則,請輸出
-1
Sample Input 1
5 20
Alice
bobby
Charlie
alicia
BOB
Li
Sample Output 1
0
Sample Input 2
4 20
Zoe
Mark
Tina
Loki
Abc
Sample Output 2
-1
Sample Input 3
7 30
Zoe
alex
Bob
carol
Amy
Banana
Nanana
Na
Sample Output 3
5
Problem 04 (10%)
You are given an integer array "
arr", of
fixed size 10, and an integer N. Your
task is to find two
different indices i and j such that:
﹥i < j
﹥the value arr[i] + arr[j] is
as close as possible to N
Formally, you want to minimize the absolute difference:
|(arr[i]+arr[j])-N|
Among all pairs (i, j) that achieve the
same minimal absolute difference, choose
the one with the
smallest i, then the smallest j.
﹥You only need to output the indices i and j.
Input:
The input consists of
two lines:
‧
Line 1: 10 integers, representing arr[0], arr[1], ..., arr[9], separated by
single spaces.
‧
Line 2: one integer N.
Output:
Output
two integers i and j (with i < j), separated by a space. These indices
must satisfy:
1. i < j,
2. | (arr[i] + arr[j]) - N |
is minimal among all 0 ≦ i < j ≦ 9,
Constraints:
‧ All elements of arr and N are
32-bit signed integers. If tied, choose the
pair
with the smallest i, then the smallest j.
Input Format
$a_0$ $a_1$ $a_2$ $a_3$ $a_4$ $a_5$ $a_6$ $a_7$ $a_8$ $a_9$
$n$
‧ 所有變數皆為介於 $-2^{31}$ 到 $2^{31}-1$ 之間的整數。
Output Format
$i_0$ $j_0$
‧ 數對 $(i_0, j_0)$ 為「滿足 $i < j$ 且 $|a_i + a_j - n|$ 最小的數對 $(i, j)$」
中,字典序最小的。
Sample Input 1
5 1 9 3 7 8 2 6 4 10
11
Sample Output 1
0 7
Sample Input 2
2 2 4 0 1 -1 -1 4 -2 0
-2
Sample Output 2
3 8
Sample Input 3
-3 8 1 5 -2 7 4 0 -1 6
3
Sample Output 3
0 9
Problem 05 (10%)
You are given a 4×4 grid representing a mini-Sudoku board. Your task is to de-
termine if the grid is a valid solved Sudoku.
A 4×4 Sudoku is valid if and only if:
1. Each
row contains the digit 1-4 exactly once.
2. Each
column contains the digits 1-4 exactly once.
3. Each of the four
2×2 sub-blocks contains the digits 1-4 exactly once.
The grid is divided into sub-blocks as follows:
‧
Top-Left: Rows 0-1, Cols 0-1
‧
Top-Right: Rows 0-1, Cols 2-3
‧
Bottom-Left: Rows 2-3, Cols 0-1
‧
Bottom-Right: Rows 2-3, Cols 2-3
https://i.imgur.com/IjjXIYp.png
\begin{tikzpicture}
\draw (0, 0) grid (4, 4);
\draw (0.5, 0.5) node{2};
\draw (0.5, 1.5) node{2};
\draw (0.5, 2.5) node{3};
\draw (0.5, 3.5) node{1};
\draw (1.5, 0.5) node{3};
\draw (1.5, 1.5) node{1};
\draw (1.5, 2.5) node{4};
\draw (1.5, 3.5) node{2};
\draw (2.5, 0.5) node{8};
\draw (2.5, 1.5) node{4};
\draw (2.5, 2.5) node{2};
\draw (2.5, 3.5) node{3};
\draw (3.5, 0.5) node{4};
\draw (3.5, 1.5) node{3};
\draw (3.5, 2.5) node{1};
\draw (3.5, 3.5) node{4};
\draw[red, very thick] (0, 2) rectangle (2, 4);
\draw[-stealth] (-1, 3) node[anchor=east]{$2\times2$ Block} -- (0, 3);
\end{tikzpicture}
Input:
The input consists of 4 lines. Each line contains 4 integers separated by single
spaces, representing the rows of the grid.
Output:
Output a single line:
‧ Print
Valid if the grid satisfies all Sudoku rules.
‧ Print
Invalid if the grid violate any rule.
Input Format
$a_{1, 1}$ $a_{1, 2}$ $a_{1, 3}$ $a_{1, 4}$
$a_{2, 1}$ $a_{2, 2}$ $a_{2, 3}$ $a_{2, 4}$
$a_{3, 1}$ $a_{3, 2}$ $a_{3, 3}$ $a_{3, 4}$
$a_{4, 1}$ $a_{4, 2}$ $a_{4, 3}$ $a_{4, 4}$
‧ 對任意 $i, j \in \{1, 2, 3, 4\}$,均有 $a_{i, j} \in \{1, 2, 3, 4\}$。
Output Format
若輸入為合法的 4×4 數獨,輸出
Valid
否則輸出
Invalid
Sample Input 1
1 2 3 4
3 4 1 2
2 1 4 3
4 3 2 1
Sample Output 1
Valid
Sample Input 2
1 2 3 4
1 2 3 4
3 4 1 2
4 3 2 1
Sample Output 2
Invalid
Sample Input 3
1 2 3 4
2 1 4 3
3 4 1 2
4 3 2 1
Sample Output 3
Invalid
--
1. 這門課黃上恩教授只是掛名;實際的授課方式是在課堂上播放陽交大温宏斌教授的課程
錄影。
2. 第二題的保留2位小數與標上
亮色的 I/O Format 都是助教 (aka 我本人) 在考試時宣布
的。
3. 每筆測試資料的執行時間限制為 1 秒;超過則該筆以 0 分計。
--
第01話 似乎在課堂上聽過的樣子 第02話 那真是太令人絕望了
第03話 已經沒什麼好期望了 第04話 被當、21都是存在的
第05話 怎麼可能會all pass 第06話 這考卷絕對有問題啊
第07話 你能面對真正的分數嗎 第08話 我,真是個笨蛋
第09話 這樣成績,教授絕不會讓我過的 第10話 再也不依靠考古題
第11話 最後留下的補考 第12話 我最愛的學分
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.249.65.236 (臺灣)
※ 文章網址: https://webptt.com/m.aspx?n=bbs/NTU-Exam/M.1767037666.A.D43.html
※ 編輯: xavier13540 (36.230.49.165 臺灣), 01/04/2026 12:50:27