NTU-Exam 板


LINE

課程名稱︰基礎程式設計(C++) 課程性質︰AI學程聯盟選修 課程教師︰温宏斌 考試日期(年月日)︰2025/10/20 考試時限(分鐘):180 試題 : Instructions: ● You may pick an arbitrary number of problems to solve with a total score of up to 100%. ● Please strictly follow the input and output specifications described in each problem. Any deviation that causes test case errors will result in point de- ductions, 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%) Sum of digits. Calculate the sum of every digit of a positive integer. 4583 → 4 + 5 + 8 + 3 = 20 Input Format $n$ 其中 $n$ 為一不超過 $10^9$ 的正整數。 Output Format $s$ 其中 $s$ 為 $n$ 的各位數字和。 Sample Input 1 4813 Sample Output 1 16 Sample Input 2 12345 Sample Output 2 15 Sample Input 3 7543824 Sample Output 3 33 Problem 02 (10%) Given two points of two rectangles, please calculate the overlapping area. Input format: The first four numbers are the x, y of the bottom left and top right points of the same rectangle. The last four numbers are the x, y of the bottom left and top right points of the other rectangle. The area will always be greater than 0. Input Format $x_1$ $y_1$ $x_2$ $y_2$ $x_3$ $y_3$ $x_4$ $y_4$ 其中 ‧ 對於所有的 $i \in \{1, 2, 3, 4\}$,均有 $x_i, y_i$ 為兩整數且 $-100 \le x_i, y_i \le 100$。 ‧ $x_1 < x_2$ 且 $y_1 < y_2$。 ‧ $x_3 < x_4$ 且 $y_3 < y_4$。 ‧ 四頂點為 $(x_1, y_1), (x_1, y_2), (x_2, y_2), (x_2, y_1)$ 的長方形與四頂點為 $(x_3, y_3), (x_3, y_4), (x_4, y_4), (x_4, y_3)$ 的長方形的交集面積必定大於 0。 Output Format $a$ 其中 $a$ 為兩長方形的交集面積。 Sample Input 1 -1 3 7 9 5 1 10 8 Sample Output 1 10 Sample Input 2 1 3 8 9 6 7 9 10 Sample Output 2 4 Sample Input 3 3 -1 7 9 -1 2 5 11 Sample Output 3 14 Problem 03 (10%) Create a C++ program that calculates the least common multiple (LCM) of two in- tegers. The inputs are two integers a and b, and a function named find_LCM will return their LCM. (The least common multiple of two positive integers is the smallest positive in- teger that is divisible by both numbers.) Hint. 最小公倍數 Input: ‧ Two integer numbers (a & b) separated by a space ‧ Both a and b are integers, and at least one of them is nonzero. (That is, you only need to handle the case where on the the numbers is 0.) Output: ‧ The least common multiple (LCM) of a and b. The result of this problem will not exceed the range of an integer. Input Format $a$ $b$ 其中 $a$ 與 $b$ 均為不超過 1000 的非負整數,且 $a\ne0$ 或 $b\ne0$。 Output Format $m$ 其中 $m$ 為 $a$ 與 $b$ 的最小公倍數,即 ‧ 若 $a\ne0$ 且 $b\ne0$,則 $m$ 為最小且同時能被 $a$ 與 $b$ 整除的正整數; ‧ 若 $a=0$ 或 $b=0$,則 $m=0$。 Sample Input 1 10 12 Sample Output 1 60 Sample Input 2 5 7 Sample Output 2 35 Sample Input 3 0 30 Sample Output 3 0 Problem 04 (10%) Create a program that converts an amount in New Taiwan Dollars (NTD) into U.S. Dollars (USD) and breaks it down into denominations. The exchange rate is 1 USD = 30 NTD. Your program should first convert the input NTD amount into USD (using floating- point division), then calculate: ‧ $a$: number of 100-dollar bills ‧ $b$: number of 10-dollar bills ‧ $c$: number of 1-dollar bills ‧ $d$: number of 10-cent coins ‧ $e$: remaining NTD after exchange ◆ Exchange rule (uniqueness requirement): Always use the largest denominations first to exchange as much as possible. For- mally, decompose the USD amount (in cents) greedily in this order: ■ $100 bills → ■ $10 bills → ■ $1 bills → ■ 10-cent coins. Finally, output the result in the following format: a = ... b = ... c = ... d = ... e = ... You can follow this flow: n = 5000 → The amount in USD (U) = n/30 = 166.66666666...... → a = integer part of U/100 = 1 → b = integer part of (U-a*100)/10 = 6 → c = integer part of (U-a*100-b*10)/1 = 6 → d = integer part of (U-a*100-b*10-c*1)*10 = 6 → e = round of (U-a*100-b*10-c*1-d*0.1)*30 = 2 Input: ◆ A single integer $n$ representing the amount in New Taiwan Dollars (NTD). ◆ n is a non-negative integer. Test cases may include values that are not divi- sible by 30. Output: ◆ The number of each denomination (a, b, c, d) and the remaining NTD (e). Input Format $n$ 其中 $n$ 為一不超過 $10^9$ 的非負整數。 Output Format a = $a$ b = $b$ c = $c$ d = $d$ e = $e$ 其中 $a, b, c, d, e$ 均為非負整數,代表 $n$ 新台幣最多能換出 $a$ 張 100 美元鈔票 ,接著再換出 $b$ 張 10 美元鈔票,接著再換出 $c$ 張 1 美元鈔票,接著再換出 $d$ 枚 10 美分硬幣,最後剩下 $e$ 新台幣。 Sample Input 1 5000 Sample Output 1 a = 1 b = 6 c = 6 d = 6 e = 2 Sample Input 2 90 Sample Output 2 a = 0 b = 0 c = 3 d = 0 e = 0 Sample Input 3 95 Sample Output 3 a = 0 b = 0 c = 3 d = 1 e = 2 Problem 05 (10%) Write a simple time-conversion program. Given a conversion type and time values, your program converts between total se- conds and hours—minutes—seconds (H:M:S) formats. There are two conversion types: ‧ Type 1: Convert from total seconds to H:M:S. ‧ Type 2: Convert from H:M:S to total seconds. The input format depends on the conversion type. Type 1 — Input should contain two integers. ‧ The first integer is 1, which represents type 1. ‧ The second integer represents the total number of seconds. Type 2 — Input should contain four integers. ‧ The first integer is 2, which represents type 2. ‧ The next three integers represent hours, minutes, and seconds, respectively. Output Format Type 1: Output the time in the format HH:MM:SS (with leading zeros, e.g., 01:01:01, 00: 59:30). Type 2: Output a single integer representing the total number of seconds. Notes: ‧ Assume all inputs are valid and non-negative ‧ Hours are not required to wrap around (Hours can be more than 24) ‧ The input fits in 32-bit signed integers ‧ Output for Type 1 must include leading zeros Input Format $t$ $a_1$ $a_2$ $\ldots$ $a_{l_t}$ 其中 $t$ 為 1 或 2 且 $(l_1, l_2) = (1, 3)$。 當 $t = 1$ 時,有 ‧ $n := a_1$ 為小於 360000 的非負整數。 當 $t = 2$ 時,有 ‧ $h := a_1$ 為小於 100 的非負整數, ‧ $m := a_2$ 為小於 60 的非負整數,且 ‧ $s := a_3$ 為小於 60 的非負整數。 Output Format 當 $t = 1$ 時,請輸出 $f_\text{02d}(h)$:$f_\text{02d}(m)$:$f_\text{02d}(s)$ 其中 ‧ $f_\text{02d}$ 為不足 2 位時在前面補 0 至 2 位的整數 formatter(e.g. $f_\text{02d}(12)$ 為 12 而 $f_\text{02d}(0)$ 為 00), ‧ $h, m, s$ 均為非負整數, ‧ $m$ 與 $s$ 均小於 60, 代表 $n$ 秒等於 $h$ 時 $m$ 分 $s$ 秒。 當 $t = 2$ 時,請輸出 $n$ 代表 $h$ 時 $m$ 分 $s$ 秒等於 $n$ 秒。 Sample Input 1 1 3661 Sample Output 1 01:01:01 Sample Input 2 2 1 1 1 Sample Output 2 3661 Sample Input 3 1 888 Sample Output 3 00:14:48 Problem 06 (10%) The number pattern problem prints a square pattern of concentric layers of num- bers. The outermost layer starts with the number n, and each inner layer de- creases the number by 1 until reaching the center, which is 1. Input Format A single integer n (1 ≦ n ≦ 9). Output Format Print the concentric square pattern of numbers. The output should be a square of size (2n-1) × (2n-1). Input Format $n$ 其中 $n$ 為不超過 9 的正整數。 Output Format $ a_{1, 1}a_{1, 2}\ldots a_{1, 2n-1}\\ a_{2, 1}a_{2, 2}\ldots a_{2, 2n-1}\\ \vdots\\ a_{2n-1, 1}a_{2n-1, 2}\ldots a_{2n-1, 2n-1} $ 其中最外圈的 $a_{i, j}$ 皆為 $n$;次外圈的 $a_{i, j}$ 皆為 $n-1$;…;最內圈(即 中央)的 $a_{i, j}$ 皆為 1。 Sample Input 1 2 Sample Output 1 222 212 222 Sample Input 2 3 Sample Output 2 33333 32223 32123 32223 33333 Sample Input 3 5 Sample Output 3 555555555 544444445 543333345 543222345 543212345 543222345 543333345 544444445 555555555 Problem 7 (10%) You're building a delivery fee calculator. The final fee should be calculated based on: (1) the price of the meal. (2) current weather conditions (raining or not). (3) the customer's VIP status. All fees are integers. Apply the steps in this order: 1. Base fee by meal price If meal_price < 200 → base fee = 100 If 200 ≦ meal_price < 500 → base fee = 50 If meal_price ≧ 500 → base fee = 20 2. Rain surcharge If is_rain = true → add 20 If is_rain = false → add 0 3. VIP discount If is_vip = true → apply 50% off to the current subtotal (integer division) If is_vip = false → no discount 4. Lower bound Ensure final fee ≧ 15 Input Format Three lines: Line 1: meal_price (integer) Line 2: is_rain (boolean: true or false, lowercase) Line 3: is_vip (boolean: true or false, lowercase) Output Format One line: the final delivery fee (integer). Constraints 0 < meal_price < 100000 is_rain ∈ {0, 1} is_vip ∈ {0, 1} Input Format $p$ $r$ $v$ 其中 ‧ $p$ 為一小於 $10^5$ 的正整數,代表餐點價格; ‧ $r$ 為 0 或 1,代表是否下雨; ‧ $v$ 為 0 或 1,代表是否為 VIP。 Output Format $f$ 其中 $f$ 為一整數,代表外送費。 Sample Input 1 180 1 0 Sample Output 1 120 Sample Input 2 520 1 1 Sample Output 2 20 Sample Input 3 250 0 0 Sample Output 3 50 Problem 08 (10%) Read an integer seed and an integer R (roll count). Initialize the C RNG with std::srand(seed) and roll a fair six-sided die exactly R times. Report: 1. the counts of each face 1..6, 2. the sample mean, 3. the unbiased sample variance. Input Format Two lines: Line 1: seed (integer) Line 2: R (number of rolls, integer) Output Format COUNTS: c1 c2 c3 c4 c5 c6 Counts of faces 1..6 over the R rools (integers). MEAN ($\bar x$): m.mm Sample mean of the R rolls, 2 decimals (小數點後 2 位), standard rounding. \[\bar x = \frac1R\sum_{i=1}^Rx_i\] MAX COUNT FACE: Print the most frequent face. MIN COUNT FACE: Print the least frequent face. For the test cases, seeds are chosen so that no two faces occur the same number of times over R rolls. Constraints ‧ 0 < seed < 1,000,000 ‧ 1 ≦ R ≦ 1,000,000 Hint: ‧ std::srand(seed) ‧ std::rand() ‧ std::setprecision(x) ‧ std::fixed (remember to #include <iomanip>) Input Format $s$ $r$ 其中 $s$ 為亂數種子,$r$ 為亂數生成次數,滿足 ‧ $s$ 為一小於 $10^6$ 的正整數, ‧ $r$ 為一不超過 $10^6$ 的正整數,且 ‧ 呼叫 std::srand($s$) 後,若令執行 $r$ 次 rand() 除以 6 餘 $k$ 的次數為 $a_k$ ,則 $a_0, a_1, \ldots, a_5$ 兩兩相異。 Output Format COUNT: $c_1$ $c_2$ $c_3$ $c_4$ $c_5$ $c_6$ MEAN: $f_\text{.2f}(\bar x)$ MAX COUNT FACE: $x_\max$ MIN COUNT FACE: $x_\min$ 其中 ‧ 對於所有的 $i \in \{1, 2, \ldots, 6\}$,均有骰子擲出 $i$ 點的次數為 $c_i$; ‧ $f_\text{.2f}$ 為一四捨五入至小數以下 2 位的浮點數 formatter(e.g. $f_\text{ .2f}(4)$ 為 4.00 而 $f_\text{.2f}(3.625)$ 為 3.63); ‧ $\bar x$ 為擲出點數的平均值; ‧ $x_\max$ 為擲出次數最多的點數; ‧ $x_\min$ 為擲出次數最少的點數。 Sample Input 1 1234 70 Sample Output 1 COUNTS: 12 11 16 14 10 7 MEAN: 3.29 MAX COUNT FACE: 3 MIN COUNT FACE: 6 Sample Input 2 228 88000 Sample Output 2 COUNTS: 14691 14812 14820 14480 14565 14632 MEAN: 3.49 MAX COUNT FACE: 3 MIN COUNT FACE: 4 Sample Input 3 7777 7777 Sample Output 3 COUNTS: 1330 1253 1265 1322 1233 1374 MEAN: 3.51 MAX COUNT FACE: 6 MIN COUNT FACE: 5 Problem 09 (10%) You have been hired by the CIA Hotel to write a simple billing program. Your task is to calculate the total amount for each guest and print a formatted table. Two monetary values (Price, Total Price) must be rounded and displayed with exa- ctly two digits after the decimal point. Input: ‧ The input consists of 3 lines in the following format: <name_1 (string)> <price_per_day_1 (double)> <days_1 (int)> <name_2 (string)> <price_per_day_2 (double)> <days_2 (int)> <name_3 (string)> <price_per_day_3 (double)> <days_3 (int)> Output Format: ‧ Print a table with the following four columns, each with a width of 10 char- acters: 。 Name — left aligned 。 Price — right aligned, two decimal places 。 Days — right aligned 。 Total — right aligned, two decimal places (Total = <price_per_day (double)> * <days (int)>) ‧ The first line should display the column headers. ‧ The next three lines should display the data for each guest. Constraints - ‧ length(name) < 7 (有誤;詳見之後的 Input Format 節) ‧ Each name consists of only one word ‧ 0 < all the input values < 100000 (有誤;詳見之後的 Input Format 節) ‧ 0 < all the calculation results < 100000 Hint: ‧ setprecision() automatically performs rounding, so you don't need to round the value manually. Input Format $s_1$ $p_1$ $d_1$ $s_2$ $p_2$ $d_2$ $s_3$ $p_3$ $d_3$ 其中對於任意 $i \in \{1, 2, 3\}$,有 ‧ $s_i$ 為一以大小寫英文字母組成且長度不超過 7 的非空字串, ‧ $p_i \in (0, 10^5)$ 為一雙精度浮點數, ‧ $d_i$ 為一不超過 $10^5$ 的正整數,且 ‧ $d_ip_i < 10^5$。 Output Format $ f_\text{-10s}(\texttt{Name})f_\text{10s}(\texttt{Price})f_\text{10s}(\texttt{ Days})f_\text{10s}(\texttt{Total})\\ f_\text{-10s}(s_1)f_\text{10.2f}(p_1)f_\text{10d}(d_1)f_\text{10.2f}(t_1)\\ f_\text{-10s}(s_2)f_\text{10.2f}(p_2)f_\text{10d}(d_2)f_\text{10.2f}(t_2)\\ f_\text{-10s}(s_3)f_\text{10.2f}(p_3)f_\text{10d}(d_3)f_\text{10.2f}(t_3) $ 其中 ‧ $f_\text{-10s}$ 為寬度為 10 且靠左對齊的字串 formatter(e.g. 若將空格換成底線 ,$f_\text{-10s}(\texttt{hello})$ 為 "hello_____"(不含引號)); ‧ $f_\text{10s}$ 為寬度為 10 且靠右對齊的字串 formatter(e.g. 若將空格換成底線 ,$f_\text{10s}(\texttt{hello})$ 為 "_____hello"(不含引號)); ‧ $f_\text{10.2f}$ 為寬度為 10、靠右對齊、四捨五入至小數以下 2 位的浮點數 form- atter(e.g. 若將空格換成底線,$f_\text{10.2f}(12.345)$ 為 "_____12.35"(不含 引號)); ‧ $f_\text{10d}$ 為寬度為 10 且靠右對齊的整數 formatter(e.g. 若將空格換成底線 ,$f_\test{10d}(12345)$ 為 "_____12345" (不含引號)); ‧ 對於任意的 $i \in \{1, 2, 3\}$,$t_i$ 為顧客 $i$ 的住宿費用。 Sample Input 1 Amy 1234.5 2 Bob 999.99 5 Clara 2500 1 Sample Output 1 Name Price Days Total Amy 1234.50 2 2469.00 Bob 999.99 5 4999.95 Clara 2500.00 1 2500.00 Sample Input 2 Zoe 88.8 10 John 10000 3 Mia 123.456 7 Sample Output 2 Name Price Days Total Zoe 88.80 10 888.00 John 10000.00 3 30000.00 Mia 123.46 7 864.19 Sample Input 3 AoA 0.00001 100000 NoN 1.111 77777 QoQ 3.333333 3 Sample Output 3 Name Price Days Total AoA 0.00 100000 1.00 NoN 1.11 77777 86410.25 QoQ 3.33 3 10.00 Problem 10 (10%) The kingdom is holding a small talent show. Each performer has a score g and a name (which may contain spaces). The king has a special number k, and he want to find the performer with the highest score that is divisible by k. Write a pro- gram to help the king find this performer. Input: ‧ The input contains several lines in the following format: 。 Line 1: an integer k 。 Starting from line 2, the input comes in pairs of lines: ■ First line of each pair: the score g (int) ■ Second line of each pair: name (string) 。 When g = 0, stop reading. Output: ‧ Print the name of the performer with the largest score divisible by k. ‧ If no score is divisible by k, print: "None". Hint: ‧ You can use the following way to assign a string value to another one: std::string a, b; std::cin >> a; b = a; // This copies the entire string stored in a to b Constraints - ◆ 0 < g, k < 10000, the last g = 0, ◆ all g values are unique ◆ Number of input lines < 50 ◆ length (each name) < 50 Attention: If the sample input lines contains trailing whitespace (行尾多餘空格) on your end, please remove it manually to avoid errors between cin and getline(). Input Format $k$ $g_1$ $s_1$ $g_2$ $s_2$ $\vdots$ $g_n$ $s_n$ $g_{n+1}$ 其中 ‧ $k$ 為一小於 10000 的正整數; ‧ 對於任意的 $i \in \{1, 2, \ldots, n\}$,均有 。 $g_i$ 為一小於 10000 的正整數,且 。 $s_i$ 為一以英數字、底線與空白組成且長度小於 50 的非空字串; ‧ $g_{n+1} = 0$,且 $g_1, g_2, \ldots, g_{n+1}$ 間兩兩相異; ‧ 輸入的行數(即 $2n+2$)小於 50。 Output Format 若有參賽者能讓國王滿意,請輸出 $s_{i^*}$ 其中 $i^*$ 滿足 ‧ $g_{i^*}$ 能被 $k$ 整除,且 ‧ 在所有能被 $k$ 整除的 $g_i$ 中,$g_{i^*}$ 是最大的。 若沒有任何參賽者能讓國王滿意,請輸出 None Sample Input 1 5 10 Alice Smith 7 Bob 15 Charlie Brown 0 Sample Output 1 Charlie Brown Sample Input 2 5 6666 Tan Gan Dee 7777 ALA Hsieh 0 Sample Output 2 None Sample Input 3 1 99 Naval_officer 1 88 Naval_officer 2 77 Naval_officer 3 66 Naval_officer 4 55 Naval_officer 5 999 Naval_officer 6 4 Naval_officer 7 2 Naval_officer 8 789 Naval_officer 9 0 Sample Output 3 Naval_officer 6 -- 1. 這門課黃上恩教授只是掛名;實際的授課方式是在課堂上播放陽交大温宏斌教授的課程 錄影。 2. 標上亮色的 Input Format 與 Output Format 都是助教 (aka 我本人) 在考試時宣布的 。 3. 部分原始 Sample Output 有誤,已修正。 -- 第01話 似乎在課堂上聽過的樣子 第02話 那真是太令人絕望了 第03話 已經沒什麼好期望了 第04話 被當、21都是存在的 第05話 怎麼可能會all pass 第06話 這考卷絕對有問題啊 第07話 你能面對真正的分數嗎 第08話 我,真是個笨蛋 第09話 這樣成績,教授絕不會讓我過的 第10話 再也不依靠考古題 第11話 最後留下的補考 第12話 我最愛的學分 --



※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.249.75.238 (臺灣)
※ 文章網址: https://webptt.com/m.aspx?n=bbs/NTU-Exam/M.1762844179.A.711.html ※ 編輯: xavier13540 (111.249.75.238 臺灣), 11/11/2025 22:03:29







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燈, 水草

請輸入看板名稱,例如:Boy-Girl站內搜尋

TOP