NTU-Exam 板


LINE

课程名称︰基础程式设计(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/cn.aspx?n=bbs/NTU-Exam/M.1767037666.A.D43.html ※ 编辑: xavier13540 (36.230.49.165 台湾), 01/04/2026 12:50:27







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Soft_Job站内搜寻

TOP