Unit 3 · For Loops and Conditions
Unit 3 · For Loops and Conditions
Last updated / 更新日期:2026-10-20
Course Home / 課程首頁 · Unit 1 / C++ 基礎
Start Here / 開始做題
- Read the input and exact output format. 中文:先找輸入、輸出及換行要求。
- Decide the start, stopping condition and update. 中文:先決定起點、停止條件及每次增減。
- Trace a small input by hand. 中文:先用小數字手算每次迴圈。
- Write, run and compare the sample; test the smallest input too. 中文:執行範例,也測試最小輸入。
Reading the Problem / 讀題須知
- Read what each input represents. N may be an upper limit, a number of values, a number of terms or a number of rows; use the meaning stated in that problem. 中文:先看 N 在該題代表甚麼,不要把「項數」與「數值上限」混為一談。
[L, R]means every integer from L to R, including L and R. Read inputs in the listed order, even when the output counts down. 中文:區間包含兩端;倒序輸出不代表把輸入次序調轉。...means the pattern continues; do not print the dots. N and sum in a mathematical expression stand for their actual values, not literal letters to print. 中文:省略號表示按規律繼續;輸出時用實際數字,不印省略號或變數名稱。- N² means N multiplied by itself. In C++, use multiplication;
^does not mean a square.10^5in a constraint means 100000, not C++ code to copy. 中文:題目中的平方及次方是數學記號,不可直接照抄成 C++ 運算。 - Output only the requested result. Do not add input prompts, labels or explanations. Use one space between adjacent numbers, no space at the start or end of a line, and a newline after the final output line, unless a problem states a different separator. 中文:只輸出指定結果;連加算式按題意使用加號,不能改為空格。
- Sample data demonstrates one case. Your program must also work for other valid inputs. 中文:範例只是其中一組資料,不可把範例的總和、次數或答案寫死。
Unit Dashboard / 單元地圖
| Learn | Use in C++ |
|---|---|
| Repeat a known number of times / 重複指定次數 | for |
| Change the step or direction / 步幅及倒數 | i += 2, i-- |
| Accumulate and count / 累加及計數 | sum += i, count++ |
| Multiply a range / 區間連乘 | long long product = 1; |
| Print an expression / 輸出算式 | separator plus accumulator / 分隔符及累加器 |
| Read repeated input / 重複讀取資料 | cin inside a loop |
| Combine conditions / 組合條件 | &&, || |
| Build rows and columns / 行與列 | nested for loops |
C++ For Loop Syntax / for 迴圈格式
Use a for loop when the number of repetitions or the range is known.
中文:知道重複次數,或知道要走訪的數字範圍時,可使用 for。
Every C++ if, else if and else branch must also use { }, even for a single statement.
中文:C++ 的每個 if、else if 及 else 分支同樣必須使用大括號。
Every for loop in this course must use { }, even when the body contains only one statement. Put { after the closing ) and } after all repeated statements.
中文:本課程所有 for 迴圈必須使用 { },即使只有一行指令也不可省略。) 後加入 {,所有重複執行的指令寫在大括號內,最後以 } 結束。
for (initialisation; condition; update) {
// statements repeated each time
}
The syntax above is a template; replace the three parts with C++ expressions. 中文:上面是語法格式,不是直接執行的程式;三部分須換成實際 C++ 語句。
for (int i = 1; i <= 5; i++) {
cout << i << '\n';
}
| Part | Meaning / 中文 | In this loop |
|---|---|---|
| Initialisation / 初始化 | Runs once before the first check / 只在開始執行一次 | int i = 1 |
| Condition / 條件 | Checked before every repetition; false stops the loop / 每次開始前檢查 | i <= 5 |
| Body / 迴圈本體 | Runs only when the condition is true / 條件成立才執行 | cout << i << '\n'; |
| Update / 更新 | Runs after the body, then returns to the condition / 本體後更新,再檢查 | i++ |
Execution order / 執行次序:initialise → check → body → update → check again.
Follow the arrows and predict what happens when i becomes 4. The diagram lists the printed values; the complete program below prints each on its own line. 中文:沿箭頭追蹤,先預測 i 變成 4 時走哪條路;圖中列出輸出數值,下方完整程式則把每個數字分行輸出。
Complete C++ Program / 完整程式
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cout << i << '\n';
}
return 0;
}
Input 3 prints 1, 2, 3 on three separate lines.
中文:輸入 3,便分三行輸出 1、2、3。
Dry Run / 完整手算追蹤
For n = 3:
| Check | i before check | i <= 3 | Output this time | i after update |
|---|---|---|---|---|
| 1 | 1 | true | 1 | 2 |
| 2 | 2 | true | 2 | 3 |
| 3 | 3 | true | 3 | 4 |
| 4 | 4 | false | no output; stop / 停止 | no update |
Loop Variations / 常用寫法
| Goal | Loop format | Values when n = 5 |
|---|---|---|
| 1 to n / 由 1 到 n | for (int i = 1; i <= n; i++) { ... } | 1, 2, 3, 4, 5 |
| Repeat n times / 做 n 次 | for (int i = 0; i < n; i++) { ... } | 0, 1, 2, 3, 4 |
| Count down / 倒數 | for (int i = n; i >= 1; i--) { ... } | 5, 4, 3, 2, 1 |
| Odd numbers / 奇數 | for (int i = 1; i <= n; i += 2) { ... } | 1, 3, 5 |
| Range / 區間 | for (int i = L; i <= R; i++) { ... } | L through R |
Accumulators, Counters and Conditions / 累加、計數及條件
| Syntax | Meaning / 中文 |
|---|---|
long long sum = 0; | Initialise once before the loop / 迴圈前初始化總和 |
sum += i; | Same as sum = sum + i; / 把本次數值加到已有總和 |
long long product = 1; | Multiplication starts at 1 / 累乘初值為 1 |
count++; | Same as count = count + 1; / 每符合一次便加一 |
i++, i--, i += 2 | Add 1, subtract 1, add 2 / 分別是每次加 1、減 1、加 2 |
product *= i; | Same as product = product * i; / 把本次數值乘入已有乘積 |
i % k == 0 | i is divisible by k; k must not be zero / 判斷倍數 |
i % a == 0 && i % b == 0 | Both conditions must be true / 同時符合 |
i % a == 0 || i % b == 0 | At least one condition is true / 至少符合一個 |
Keep sum and count outside the loop so they are not reset on each repetition.
中文:總和及計數器在迴圈外初始化;放在裡面會每次歸零。大數總和、階乘及平方使用 long long;平方可寫 1LL * i * i。
1LL is the integer 1 stored as long long. In 1LL * i * i, multiplication starts with that wider type. Storing the final result in long long alone does not prevent an earlier int multiplication from overflowing.
中文:1LL 令乘法從一開始就以 long long 計算;只把最後結果存入 long long,不能挽救乘法途中已發生的溢位。
Trace the same values 1, 2, 3, 4. Say which result would be wrong if its initial value were 0. 中文:用同一組數字比較兩種更新方式,再指出哪種運算不能以 0 作初值。
Conditions inside a Loop / 迴圈內的條件判斷
Check one score on each repetition. The condition decides whether the counter changes; both paths continue after the branch. 中文:每次迴圈檢查一個分數;只有符合條件才增加人數,兩條路最後都會繼續。
Output Separators / 空格與換行
For a sequence, print a space before every item except the first. For filtered values, use a bool first = true flag and change it after printing the first match. Print the final newline even when no value matches.
中文:第一個數字前不印空格;篩選數列可用 first 標記。沒有符合數字時,數列那一行仍須輸出換行。
bool stores true or false. Here, first means “no matching value has been printed yet”.
中文:first 起初為 true,表示還未印出第一個符合條件的數字。
| Matching value | first before printing | What to print | first afterwards |
|---|---|---|---|
| 2 (first match) | true | 2, with no leading space | false |
| 4 (next match) | false | one space, then 4 | false |
If a task asks for a list, count and sum but no values match, the list line is blank and the count and sum are both 0. A blank line means printing a newline, not the words “blank line”. 中文:題目若要求「數列、數量、總和」,但沒有符合的數字,仍輸出三行:第一行留空,第二、三行各輸出 0;不要印出「空白行」三個字。
Nested For Loops / 巢狀迴圈
Point to where the inner loop restarts and where the newline is printed. 中文:指出每行內層迴圈的起點,以及換行應在甚麼時候發生。
for (int row = 1; row <= 3; row++) {
for (int col = 1; col <= row; col++) {
cout << '*';
}
cout << '\n';
}
The outer loop selects a row; the inner loop finishes that row. Put the row newline after the inner loop.
中文:外層決定行,內層完成該行;換行放在內層結束後。輸出為三行:*、**、***。
Common Errors / 常見錯誤
| Mistake | Correction / 修正 |
|---|---|
A semicolon between ) and { | Remove that semicolon and keep { }; it creates an empty loop body / ) 與 { 之間不可加分號,且必須保留大括號 |
i < n when n must be printed | Use i <= n / 包含終點 |
Counting down with i++ | Use i-- and a suitable condition / 更新方向須正確 |
Resetting sum = 0 inside the loop | Initialise before the loop / 不要每次歸零 |
| Product starts at 0 | Start at 1 / 乘積初值為 1 |
100 <= x <= 999 | Use x >= 100 && x <= 999 / 不用連鎖比較 |
i ^ 2 for a square | Use 1LL * i * i; ^ is XOR / 平方不是 XOR |
Using i after for (int i = ...; ...; ...) { ... } | i belongs to that loop's scope / i 只在該迴圈有效 |
Supplement · Taxi Fare in C++ (if…else) / C++ 的士收費題目
Write a program in C++ to calculate and display the taxi fare using if...else.
中文:使用 C++ 的 if...else 編寫程式,輸入行駛里程(米),計算並輸出的士車資。
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | For this exercise, the base fare is MOP 20 for the first 1500 metres. Beyond 1500 metres, add MOP 2 for every additional 500 metres or part thereof. 中文:首 1500 米收費 MOP 20;超過 1500 米後,每增加 500 米加收 MOP 2,不足 500 米亦按 500 米計算。 |
| Input Format / 輸入格式 | One non-negative integer: mileage in metres. 中文:輸入一個非負整數,表示行駛里程(米)。 |
| Output Format / 輸出格式 | Print MOP followed by one space and the integer fare, for example MOP 26.中文:輸出 MOP、一個空格及整數車資,不需輸出提示文字。 |
| Constraints / 限制 | Mileage is a non-negative whole number of metres. 中文:里程為非負整數;剛好 1500 米仍收 MOP 20。 |
Sample Cases / 範例
| Input / 輸入 | Output / 輸出 | Calculation / 計算 |
|---|---|---|
| 2600 | MOP 26 | 1100 extra metres → 3 blocks; 20 + 3 × 2 = 26 / 超出 1100 米,按 3 段計算 |
| 2000 | MOP 22 | 500 extra metres → 1 block; 20 + 2 = 22 / 超出 500 米,按 1 段計算 |
| 100 | MOP 20 | Within the first 1500 metres / 未超過首 1500 米 |
Sample Input / 範例輸入
2600
Sample Output / 範例輸出
MOP 26
Reference Answer / C++ 參考答案
#include <iostream>
using namespace std;
int main() {
long long distance;
cin >> distance;
long long fare;
if (distance <= 1500) {
fare = 20;
} else {
long long extra_distance = distance - 1500;
long long blocks = (extra_distance + 499) / 500;
fare = 20 + blocks * 2;
}
cout << "MOP " << fare << '\n';
return 0;
}
How It Works / 計算說明
cin >> distance;: read the distance in metres. 中文:輸入整數里程(米)。if (distance <= 1500) { ... }: use the base fare of 20. 中文:未超過 1500 米,收基本車資 20。else { ... }: calculate only the distance beyond 1500 metres. 中文:超過起步里程後,只計算超出的部分。(extra_distance + 499) / 500: both operands are integers, so/performs integer division. Adding 499 first counts an incomplete positive 500-metre block as a whole block. 中文:兩邊都是整數,/進行整數除法;先加 499,令不足 500 米的部分亦計作一段。cout << "MOP " << fare << '\n';: print MOP, one space and the fare. 中文:輸出貨幣單位、一個空格及車資,最後換行。
Boundary Checks / 邊界檢查
| Input / 輸入 | Output / 輸出 | Reason / 原因 |
|---|---|---|
| 1500 | MOP 20 | Exactly the base distance / 剛好起步里程 |
| 1501 | MOP 22 | The first extra block has started / 已開始第一段額外里程 |
| 2001 | MOP 24 | The second extra block has started / 已開始第二段額外里程 |
Examples / 例題
Each reference answer is a separate complete program. 中文:每個參考答案都是獨立完整程式,逐題複製執行。
Example 3-1 · Two Sequences
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Print two sequences: first 1, 2, 3, … up to 100; then start at 1 and add 3 each time, ending at 100. 中文:第一行由 1 開始每次加 1;第二行由 1 開始每次加 3,兩者都包含 100。 |
| Input Format / 輸入格式 | This problem does not require standard input. 中文:不需要標準輸入。 |
| Output Format / 輸出格式 | Print each sequence on its own line; numbers are separated by one space. 中文:每列數字以一個空格分隔。 |
| Constraints / 限制 | No input. |
Sample Input / 範例輸入
(no standard input)
Sample Output / 範例輸出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
1 4 7 10 13 16 19 22 25 28 31 34 37 40 43 46 49 52 55 58 61 64 67 70 73 76 79 82 85 88 91 94 97 100
C++ focus / 語法重點:Use two separate for loops with { }; finish the first output line before starting the second / 兩個迴圈各輸出一行,數字之間只有一個空格。
Reference Answer / 參考答案
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 100; i++) {
if (i > 1) {
cout << ' ';
}
cout << i;
}
cout << '\n';
for (int i = 1; i <= 100; i += 3) {
if (i > 1) {
cout << ' ';
}
cout << i;
}
cout << '\n';
return 0;
}
Example 3-2 · Sum from 1 to N
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Given N, calculate 1 + 2 + … + N. 中文:計算 1 到 N 的總和。 |
| Input Format / 輸入格式 | One integer N. 中文:一個整數 N。 |
| Output Format / 輸出格式 | Print the sum. 中文:輸出總和。 |
| Constraints / 限制 | 1 ≤ N ≤ 100000 |
Sample Input / 範例輸入
10
Sample Output / 範例輸出
55
C++ focus / 語法重點:An accumulator keeps a running total. Initialise long long sum = 0; before the loop, then add each value / 累加器保存已有總和,在迴圈外初始化,避免每次歸零。
Reference Answer / 參考答案
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
long long sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
cout << sum << '\n';
return 0;
}
Example 3-3 · Count Multiples
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Given L, R and K, count integers in [L, R] divisible by K. 中文:統計區間內 K 的倍數。 |
| Input Format / 輸入格式 | Three integers in this order: L, R, K. 中文:依次讀入左端 L、右端 R、倍數 K。 |
| Output Format / 輸出格式 | Print the count. 中文:輸出數量。 |
| Constraints / 限制 | 1 ≤ L ≤ R ≤ 10^5; 1 ≤ K ≤ 10^5 |
Sample Input / 範例輸入
1 20 3
Sample Output / 範例輸出
6
C++ focus / 語法重點:Use i % K == 0 to identify a multiple; increase the count by 1, not by i / 找到一個倍數就把數量加 1,不是加上該數值。
Reference Answer / 參考答案
#include <iostream>
using namespace std;
int main() {
int l, r, k, count = 0;
cin >> l >> r >> k;
for (int i = l; i <= r; i++) {
if (i % k == 0) {
count++;
}
}
cout << count << '\n';
return 0;
}
Example 3-4 · Print 1 to N / 輸出 1 到 N
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Print every integer from 1 to N on one line. 中文:順序輸出 1 至 N。 |
| Input Format / 輸入格式 | One integer N. |
| Output Format / 輸出格式 | Numbers separated by one space; no trailing space. |
| Constraints / 限制 | 1 ≤ N ≤ 100 |
Sample Input / 範例輸入
5
Sample Output / 範例輸出
1 2 3 4 5
C++ focus / 語法重點:Initialise i at 1 and include N. Print a space only before the second and later values / 包含終點 N,第一個數字前不印空格。
Reference Answer / 參考答案
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
if (i > 1) {
cout << ' ';
}
cout << i;
}
cout << '\n';
return 0;
}
Example 3-5 · Countdown / 倒數
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Print N down to 1. 中文:由 N 倒數至 1。 |
| Input Format / 輸入格式 | One integer N. |
| Output Format / 輸出格式 | One line, separated by one space. |
| Constraints / 限制 | 1 ≤ N ≤ 100 |
Sample Input / 範例輸入
4
Sample Output / 範例輸出
4 3 2 1
C++ focus / 語法重點:Start at N and subtract 1 each time; the last printed value is 1 / 由 N 開始,每次減 1,最後印出 1。
Reference Answer / 參考答案
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = n; i >= 1; i--) {
if (i < n) {
cout << ' ';
}
cout << i;
}
cout << '\n';
return 0;
}
Example 3-6 · Sum of Even Numbers / 偶數總和
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Sum the even integers from 1 to N. 中文:加總 1 至 N 的偶數。 |
| Input Format / 輸入格式 | One integer N, the upper limit of the values to consider. 中文:N 是數值上限,不是偶數的個數。 |
| Output Format / 輸出格式 | Print only the sum on one line. If N=1, print 0. 中文:只輸出總和;N=1 時沒有正偶數,輸出 0。 |
| Constraints / 限制 | 1 ≤ N ≤ 100000 |
Sample Input / 範例輸入
10
Sample Output / 範例輸出
30
C++ focus / 語法重點:Start with the first positive even number and add 2 each time / 從第一個正偶數開始,每次加 2;N 是上限。
Reference Answer / 參考答案
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
long long sum = 0;
for (int i = 2; i <= n; i += 2) {
sum += i;
}
cout << sum << '\n';
return 0;
}
Example 3-7 · Sum N Inputs / 加總 N 個輸入
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Read N integers and print their sum. 中文:讀取 N 個整數並加總。 |
| Input Format / 輸入格式 | Line 1: N, the number of values. Line 2: exactly N integers to add. 中文:第一行 N 是數量;第二行才是要加總的 N 個整數,N 本身不計入總和。 |
| Output Format / 輸出格式 | Print the sum. |
| Constraints / 限制 | 1 ≤ N ≤ 100000; each value is between -1000000 and 1000000 |
Sample Input / 範例輸入
4
8 -2 5 1
Sample Output / 範例輸出
12
C++ focus / 語法重點:Read each value inside the loop and add it to the existing total / 迴圈內每次讀取一個數並累加,不能把所有輸入都放在迴圈之前。
Reference Answer / 參考答案
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
long long sum = 0;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
sum += x;
}
cout << sum << '\n';
return 0;
}
Example 3-8 · Count Passing Scores / 計算合格人數
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Among N scores, count how many are 50 or higher. A score of exactly 50 counts as passing. 中文:統計不少於 50 分的人數,50 分也合格。 |
| Input Format / 輸入格式 | Line 1: N. Line 2: exactly N integer scores. 中文:N 是分數的數量,不是其中一個分數。 |
| Output Format / 輸出格式 | Print the passing count. |
| Constraints / 限制 | 1 ≤ N ≤ 1000; 0 ≤ score ≤ 100 |
Sample Input / 範例輸入
5
49 50 75 0 100
Sample Output / 範例輸出
3
C++ focus / 語法重點:Check each score inside the loop. Increase the counter only in the matching if branch, with { } / 每個分數都要判斷,只有合格時才把人數加一。
Reference Answer / 參考答案
#include <iostream>
using namespace std;
int main() {
int n, count = 0;
cin >> n;
for (int i = 0; i < n; i++) {
int score;
cin >> score;
if (score >= 50) {
count++;
}
}
cout << count << '\n';
return 0;
}
Example 3-9 · Rectangle of Stars / 星號長方形
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Print R rows of C stars. 中文:輸出 R 行,每行 C 個星號。 |
| Input Format / 輸入格式 | Two integers in this order: R (number of rows), then C (stars per row). 中文:先讀入行數 R,再讀入每行星號數 C。 |
| Output Format / 輸出格式 | R lines; no spaces between stars. |
| Constraints / 限制 | 1 ≤ R, C ≤ 20 |
Sample Input / 範例輸入
2 4
Sample Output / 範例輸出
****
****
C++ focus / 語法重點:The outer loop selects a row; the inner loop prints all C stars. Put the newline after the inner loop / 外層處理行,內層印完該行星號後才換行。
Reference Answer / 參考答案
#include <iostream>
using namespace std;
int main() {
int r, c;
cin >> r >> c;
for (int row = 1; row <= r; row++) {
for (int col = 1; col <= c; col++) {
cout << '*';
}
cout << '\n';
}
return 0;
}
Example 3-10 · Number Triangle / 數字三角形
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Print N rows, numbered from 1 to N. Row i contains the numbers 1 to i. 中文:行號由 1 開始;第 i 行有 i 個數字,依次為 1 至 i。 |
| Input Format / 輸入格式 | One integer N. |
| Output Format / 輸出格式 | N lines; numbers separated by one space. |
| Constraints / 限制 | 1 ≤ N ≤ 9 |
Sample Input / 範例輸入
4
Sample Output / 範例輸出
1
1 2
1 2 3
1 2 3 4
C++ focus / 語法重點:The inner loop ends at the current row number, not always at N / 內層上限隨目前行號改變,不是每行都印 N 個數字。
Reference Answer / 參考答案
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int row = 1; row <= n; row++) {
for (int col = 1; col <= row; col++) {
if (col > 1) {
cout << ' ';
}
cout << col;
}
cout << '\n';
}
return 0;
}
Example 3-11 · Product from L to R / 區間連乘
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Given L and R, multiply every integer from L to R, including both endpoints. 中文:計算 L × (L+1) × … × R,包含兩端。 |
| Input Format / 輸入格式 | Two integers in this order: L, then R. 中文:先輸入 L,再輸入 R。 |
| Output Format / 輸出格式 | Print the integer product on one line; do not print prompts. 中文:輸出整數乘積,不加提示文字。 |
| Constraints / 限制 | 1 ≤ L ≤ R ≤ 20; use long long for the product. |
Sample Input / 範例輸入
3 5
Sample Output / 範例輸出
60
C++ focus / 語法重點:Initialise product to 1 before the loop. Use the input bounds rather than fixed sample values. Every loop uses { }.
中文:乘積在迴圈外設為 1;起點及終點來自輸入,不可固定為範例的 3 和 5。
For L=3 and R=5: product changes from 1 → 3 → 12 → 60. If L=R, multiply that number once.
中文:若兩端相同,仍要執行一次;乘積初值若設為 0,結果會一直是 0。
Reference Answer / 參考答案
#include <iostream>
using namespace std;
int main() {
int l, r;
cin >> l >> r;
long long product = 1;
for (int i = l; i <= r; i++) {
product *= i;
}
cout << product << '\n';
return 0;
}
Example 3-12 · Addition Expression for Multiples / 倍數連加算式
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Given N and K, print the first N positive multiples of K as an addition expression, followed by = and their sum.中文:把 K 的前 N 個正倍數寫成連加算式,最後輸出等號及實際總和。 |
| Input Format / 輸入格式 | Two integers in this order: N, then K. 中文:先輸入數量 N,再輸入倍數 K。 |
| Output Format / 輸出格式 | One line such as 3+6+9+12=30. Use no spaces and no + before =. If N=1, print one term and its sum, such as 7=7.中文:一行輸出,不留空格,等號前不可多一個加號;總和須計算,不可寫死。 |
| Constraints / 限制 | 1 ≤ N, K ≤ 100 |
Sample Input / 範例輸入
4 3
Sample Output / 範例輸出
3+6+9+12=30
C++ focus / 語法重點:For each i, the term is k * i. Start sum at 0, print + before every term except the first, and print = after the loop.
中文:第 i 項為 k × i;每次都累加該項,不是把總和固定設為範例的 30。
The picture uses N=3 and K=2; the sample above uses N=4 and K=3. Trace both with the same steps. 中文:圖解使用另一組 N=3、K=2;上方範例為 N=4、K=3,兩者使用同一套輸出步驟。
For N=4 and K=3, the terms are 3, 6, 9, 12 and sum changes 0 → 3 → 9 → 18 → 30. All statements between { and } repeat; the final = and total print once after }.
中文:大括號內的指令每次重複;等號及最後總和在迴圈結束後只輸出一次。
Reference Answer / 參考答案
#include <iostream>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
long long sum = 0;
for (int i = 1; i <= n; i++) {
int value = k * i;
if (i > 1) {
cout << '+';
}
cout << value;
sum += value;
}
cout << '=' << sum << '\n';
return 0;
}
ClassWork 3 / 課堂練習
Complete the assigned tasks independently. Submit source code and a screenshot of a successful run. 中文:獨立完成老師指定題目,提交原始碼及成功輸出截圖。
All exercises in this unit use ClassWork 3-x. Start with the basic loop tasks, then practise accumulation, conditions and nested loops. 中文:本章統一使用 ClassWork 3-x;先練習基本迴圈,再做累加、條件及巢狀迴圈。
| Practice stage / 練習階段 | Tasks / 題號 |
|---|---|
| Basic loop control / 基本迴圈 | 3-10 to 3-13 |
| Accumulation and input / 累加及輸入 | 3-2, 3-7, 3-14 to 3-17 |
| Conditions, counts and divisibility / 條件、計數及整除 | 3-1, 3-3, 3-4, 3-18, 3-19 |
| Nested loops / 巢狀迴圈 | 3-8, 3-9, 3-20 |
| Extension and revision / 延伸及複習 | 3-5; 3-6 (BMI arithmetic revision / 算術複習) |
ClassWork 3-1 · List Multiples of 7
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Given L and R, print all multiples of 7 in [L, R], then print their count and sum. 中文:列出、統計及加總 7 的倍數。 |
| Input Format / 輸入格式 | Two integers L and R. 中文:兩個整數 L、R。 |
| Output Format / 輸出格式 | Line 1: matching values in increasing order, separated by one space; print a blank line if there are none. Line 2: count. Line 3: sum. 中文:第一行數列,無符合數字則留空行;第二行數量;第三行總和。 |
| Constraints / 限制 | 1 ≤ L ≤ R ≤ 10^5 |
Sample Input / 範例輸入
1 30
Sample Output / 範例輸出
7 14 21 28
4
70
Hint / 提示:Maintain both count and sum.
ClassWork 3-2 · Factorial
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Given N, calculate N! = 1 × 2 × … × N. 中文:計算階乘。 |
| Input Format / 輸入格式 | One integer N. 中文:一個整數 N。 |
| Output Format / 輸出格式 | Print N!. 中文:輸出 N 的階乘。 |
| Constraints / 限制 | 1 ≤ N ≤ 20; use long long. |
Sample Input / 範例輸入
5
Sample Output / 範例輸出
120
Hint / 提示:Start the product at 1.
ClassWork 3-3 · Multiples of Both A and B / 同時為 A、B 的倍數
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Given L, R, A and B, print numbers divisible by both A and B, then print their count and sum. 中文:找同時可被 A、B 整除的數。 |
| Input Format / 輸入格式 | Four integers L, R, A and B. 中文:四個整數 L、R、A、B。 |
| Output Format / 輸出格式 | Line 1: matching values in increasing order, separated by one space; print a blank line if there are none. Line 2: count. Line 3: sum. 中文:第一行數列,無符合數字則留空行;第二行數量;第三行總和。 |
| Constraints / 限制 | 1 ≤ L ≤ R ≤ 100000; 1 ≤ A, B ≤ 100000 |
Sample Input / 範例輸入
1 100 5 11
Sample Output / 範例輸出
55
1
55
Hint / 提示:Use logical AND: i % A == 0 && i % B == 0.
ClassWork 3-4 · Divisible by A or B
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Given L, R, A and B, list values divisible by A or B, then print count and sum. A value divisible by both is listed once. 中文:找可被 A 或 B 整除的數。 |
| Input Format / 輸入格式 | Four integers L, R, A and B. 中文:四個整數 L、R、A、B。 |
| Output Format / 輸出格式 | Line 1: matching values in increasing order, separated by one space; print a blank line if there are none. Line 2: count. Line 3: sum. 中文:第一行數列,無符合數字則留空行;第二行數量;第三行總和。 |
| Constraints / 限制 | 1 ≤ L ≤ R ≤ 100000; 1 ≤ A, B ≤ 100000 |
Sample Input / 範例輸入
1 30 13 29
Sample Output / 範例輸出
13 26 29
3
68
Hint / 提示:Use logical OR: ||.
ClassWork 3-5 · Alternating Squares
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Given A and B, calculate A² − (A+1)² + (A+2)² − … ± B², beginning with plus for A. 中文:由 A² 開始,依次加、減各整數的平方;第一項永遠是正號,不按 A 的奇偶決定。 |
| Input Format / 輸入格式 | Two integers A and B. 中文:兩個整數 A、B。 |
| Output Format / 輸出格式 | Print the result. 中文:輸出結果。 |
| Constraints / 限制 | 1 ≤ A ≤ B ≤ 10000 |
Sample Input / 範例輸入
2 7
Sample Output / 範例輸出
-27
Hint / 提示:Use parity of i - A to choose + or −.
ClassWork 3-6 · BMI
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Given weight in kilograms and height in metres, calculate BMI = weight / height². 中文:計算身體質量指數 BMI。 |
| Input Format / 輸入格式 | Two real numbers in this order: weight in kilograms, then height in metres. 中文:先輸入體重(公斤),再輸入身高(米),不可調換次序。 |
| Output Format / 輸出格式 | Print BMI with exactly 2 decimal places. 中文:固定輸出兩位小數。 |
| Constraints / 限制 | 0 < weight, height ≤ 300 |
Sample Input / 範例輸入
60 1.75
Sample Output / 範例輸出
19.59
Hint / 提示:Use double, include <iomanip>, and use fixed with setprecision(2) / 以 double 計算,再固定輸出兩位小數。 Arithmetic and decimal-format revision; no loop is needed / 此題複習算術及小數格式,不需要迴圈。
ClassWork 3-7 · Addition Expression
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Given N, print 1+2+...+N=sum.中文:輸出連加算式及總和。 |
| Input Format / 輸入格式 | One integer N. 中文:一個整數 N。 |
| Output Format / 輸出格式 | Print the complete expression on one line with no spaces. Put = after N and then the calculated sum; do not print a final +. For N=1, print 1=1.中文:輸出完整算式,不留空格;最後一個數字後是等號及計算所得的總和,不是加號。N=1 時輸出 1=1。 |
| Constraints / 限制 | 1 ≤ N ≤ 10000 |
Sample Input / 範例輸入
4
Sample Output / 範例輸出
1+2+3+4=10
Hint / 提示:Decide whether to print + before or after each number.
ClassWork 3-8 · Multiplication Table
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Given N, print the N × N multiplication table. 中文:輸出 N × N 乘法表。 |
| Input Format / 輸入格式 | One integer N. 中文:一個整數 N。 |
| Output Format / 輸出格式 | Print N rows with N values per row. The value in row i, column j is i × j; row and column numbers start at 1. Separate adjacent values with one space. 中文:輸出 N 行、每行 N 個數字;第 i 行第 j 個值為 i × j,行號及列號都由 1 開始。 |
| Constraints / 限制 | 1 ≤ N ≤ 9 |
Sample Input / 範例輸入
3
Sample Output / 範例輸出
1 2 3
2 4 6
3 6 9
Hint / 提示:Use nested for loops.
ClassWork 3-9 · Right Triangle
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Given N, print a right triangle of * with N rows.中文:輸出直角星號三角形。 |
| Input Format / 輸入格式 | One integer N. 中文:一個整數 N。 |
| Output Format / 輸出格式 | Print N lines. Line i contains exactly i stars, starting with one star on line 1; use no spaces between stars or before them. 中文:第一行 1 個星號,第 i 行 i 個;星號前及星號之間都沒有空格。 |
| Constraints / 限制 | 1 ≤ N ≤ 50 |
Sample Input / 範例輸入
4
Sample Output / 範例輸出
*
**
***
****
Hint / 提示:Outer loop controls rows; inner loop controls stars.
ClassWork 3-10 · Repeat a Message / 重複文字
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Print Hello, F3! N times.中文:重複輸出指定文字 N 次。 |
| Input Format / 輸入格式 | One integer N. |
| Output Format / 輸出格式 | N lines; each exactly Hello, F3!, including the comma, the space after it, the capital H and F, and the exclamation mark.中文:大小寫、逗號、逗號後的空格及驚嘆號都須相同。 |
| Constraints / 限制 | 1 ≤ N ≤ 20 |
Sample Input / 範例輸入
3
Sample Output / 範例輸出
Hello, F3!
Hello, F3!
Hello, F3!
Hint / 提示:Repeat N times / 執行 N 次。
ClassWork 3-11 · Ascending Range / 遞增區間
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Print every integer from L to R inclusive. 中文:由 L 順序輸出至 R。 |
| Input Format / 輸入格式 | Two integers in this order: L, then R, with L ≤ R. 中文:先讀入較小端 L,再讀入較大端 R;不要因輸出方向而調換讀入次序。 |
| Output Format / 輸出格式 | One line with one space between numbers. |
| Constraints / 限制 | -100 ≤ L ≤ R ≤ 100 |
Sample Input / 範例輸入
-2 2
Sample Output / 範例輸出
-2 -1 0 1 2
Hint / 提示:Include both endpoints / 包含兩端。
ClassWork 3-12 · Descending Range / 遞減區間
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Print integers from R down to L inclusive. 中文:由 R 倒數至 L。 |
| Input Format / 輸入格式 | Two integers in this order: L, then R, with L ≤ R. 中文:先讀入較小端 L,再讀入較大端 R;不要因輸出方向而調換讀入次序。 |
| Output Format / 輸出格式 | One line with one space between numbers. |
| Constraints / 限制 | -100 ≤ L ≤ R ≤ 100 |
Sample Input / 範例輸入
2 6
Sample Output / 範例輸出
6 5 4 3 2
Hint / 提示:Use i– / 每次減一。
ClassWork 3-13 · Odd Numbers / 奇數數列
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Print all odd positive integers no greater than N. 中文:輸出不大於 N 的正奇數。 |
| Input Format / 輸入格式 | One integer N, the upper limit, not the number of odd values. 中文:N 是數值上限,不是要輸出的奇數個數。 |
| Output Format / 輸出格式 | One line separated by single spaces. |
| Constraints / 限制 | 1 ≤ N ≤ 100 |
Sample Input / 範例輸入
9
Sample Output / 範例輸出
1 3 5 7 9
Hint / 提示:Start at 1 and step by 2 / 從 1 開始,每次加 2。
ClassWork 3-14 · Sum of Squares / 平方總和
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Calculate 1² + 2² + … + N². 中文:計算 1 至 N 的平方總和。 |
| Input Format / 輸入格式 | One integer N. |
| Output Format / 輸出格式 | Print the integer sum. |
| Constraints / 限制 | 1 ≤ N ≤ 100000; use long long |
Sample Input / 範例輸入
4
Sample Output / 範例輸出
30
Hint / 提示:Use 1LL * i * i / 先以 long long 相乘。
ClassWork 3-15 · Count Positive, Negative and Zero / 正負及零計數
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Classify N integers as positive, negative or zero. 中文:統計正數、負數及零。 |
| Input Format / 輸入格式 | Line 1: N, the number of values. Line 2: exactly N integers. 中文:第一行是數量 N,第二行是 N 個數值;N 不算其中一個數值。 |
| Output Format / 輸出格式 | Print positive count, negative count and zero count on one line, separated by spaces. |
| Constraints / 限制 | 1 ≤ N ≤ 1000; -1000000 ≤ value ≤ 1000000 |
Sample Input / 範例輸入
6
-3 0 5 2 -1 0
Sample Output / 範例輸出
2 2 2
Hint / 提示:Use if / else if / else with three counters / 使用三個計數器。
ClassWork 3-16 · Passing Count and Score Sum / 合格人數與總分
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | For N scores, count those at least 50 and sum only the passing scores. 中文:只計算合格分數的人數與總分。 |
| Input Format / 輸入格式 | Line 1: N. Line 2: exactly N integer scores. 中文:第一行是分數數量,第二行是 N 個分數。 |
| Output Format / 輸出格式 | Line 1: passing count. Line 2: sum of passing scores. Scores of exactly 50 are included. If no one passes, print 0 on each of the two lines. 中文:50 分也計算在內;若沒有人合格,兩行各輸出 0。 |
| Constraints / 限制 | 1 ≤ N ≤ 1000; 0 ≤ score ≤ 100 |
Sample Input / 範例輸入
5
49 50 75 0 100
Sample Output / 範例輸出
3
225
Hint / 提示:Update both values only for a passing score / 合格才計數及累加。
ClassWork 3-17 · Largest Input / 最大值
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Find the largest of N integers. 中文:找出 N 個整數的最大值。 |
| Input Format / 輸入格式 | Line 1: N, the number of values. Line 2: exactly N integers. 中文:第一行是數量 N,第二行是 N 個數值;N 不算其中一個數值。 |
| Output Format / 輸出格式 | Print the maximum. |
| Constraints / 限制 | 1 ≤ N ≤ 1000; -1000000 ≤ value ≤ 1000000 |
Sample Input / 範例輸入
4
-8 -2 -5 -11
Sample Output / 範例輸出
-2
Hint / 提示:Use the first value as the initial maximum, then compare the remaining N−1 values. For -8, -2, -5, the maximum changes -8 → -2 → -2. If N=1, the only value is the maximum.
中文:全部數字都可能是負數,不能把最大值預設為 0;第一個數讀取後,不要再多讀 N 個數。
ClassWork 3-18 · Divisor List / 因數列表
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | List all positive divisors of N and count them. 中文:列出能整除 N 的所有正整數及其數量,包含 1 和 N。 |
| Input Format / 輸入格式 | One integer N. |
| Output Format / 輸出格式 | Line 1: divisors in increasing order separated by spaces. Line 2: count. |
| Constraints / 限制 | 1 ≤ N ≤ 100000 |
Sample Input / 範例輸入
12
Sample Output / 範例輸出
1 2 3 4 6 12
6
Hint / 提示:Test N % i == 0 / 逐一檢查整除。
ClassWork 3-19 · Count Multiples of Exactly One / 只符合一個倍數條件
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Count integers in [L,R] divisible by A or B but not both. 中文:只計算「可被 A 整除但不可被 B 整除」,或「可被 B 整除但不可被 A 整除」的數;同時可被兩者整除的不計算。 |
| Input Format / 輸入格式 | Four integers L, R, A and B. |
| Output Format / 輸出格式 | Print the count. |
| Constraints / 限制 | 1 ≤ L ≤ R ≤ 100000; 1 ≤ A, B ≤ 100000 |
Sample Input / 範例輸入
1 12 2 3
Sample Output / 範例輸出
6
Hint / 提示:Use two bool conditions and compare them with != / 兩條件結果不同才計數。
ClassWork 3-20 · Inverted Star Triangle / 倒星號三角形
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Print N stars on the first row, N-1 on the next, down to 1. 中文:由 N 個星號逐行減至 1 個。 |
| Input Format / 輸入格式 | One integer N. |
| Output Format / 輸出格式 | N lines; no leading spaces, spaces between stars or blank rows. |
| Constraints / 限制 | 1 ≤ N ≤ 50 |
Sample Input / 範例輸入
4
Sample Output / 範例輸出
****
***
**
*
Hint / 提示:Decrease the inner bound each row / 每行減少內層次數。
ClassWork 3-21 · Sum of Odd Numbers / 奇數總和
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Given N, calculate the sum of all positive odd integers no greater than N. 中文:加總不大於 N 的所有正奇數。 |
| Input Format / 輸入格式 | One integer N, the upper limit of the values to consider. 中文:N 是數值上限,不是要加總的項數。 |
| Output Format / 輸出格式 | Print the integer sum on one line. 中文:一行輸出總和,不輸出數列或提示文字。 |
| Constraints / 限制 | 1 ≤ N ≤ 100000; use long long for the sum. |
| Difficulty / 難度 | Easy |
Sample Input / 範例輸入
9
Sample Output / 範例輸出
25
Hint / 提示:Decide the first value and the step / 先決定第一個數及每次的步幅。
ClassWork 3-22 · Sum of Multiples of 3 / 三的倍數總和
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Given N, calculate the sum of all positive multiples of 3 no greater than N. 中文:加總不大於 N 的所有正整數 3 的倍數。 |
| Input Format / 輸入格式 | One integer N, the upper limit of the values to consider. 中文:N 是數值上限,不是要加總的項數。 |
| Output Format / 輸出格式 | Print the sum on one line. If no value matches, print 0. 中文:一行輸出總和;沒有符合的數字時輸出 0。 |
| Constraints / 限制 | 1 ≤ N ≤ 100000; use long long for the sum. |
| Difficulty / 難度 | Easy |
Sample Input / 範例輸入
10
Sample Output / 範例輸出
18
Hint / 提示:Check whether the loop should run when N is 1 or 2 / 留意 N=1 或 2 時的結果。
ClassWork 3-23 · Total Spending / 支出總額
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Read the spending amount for each of N days and calculate the total. 中文:讀入 N 天各天的整數支出金額,計算總支出。 |
| Input Format / 輸入格式 | Line 1: N. Line 2: N integers representing the daily amounts. 中文:第一行為 N,第二行為 N 個金額。 |
| Output Format / 輸出格式 | Print the total amount as an integer on one line. 中文:一行輸出整數總額。 |
| Constraints / 限制 | 1 ≤ N ≤ 100000; 0 ≤ each amount ≤ 1000000; use long long for the total. |
| Difficulty / 難度 | Easy |
Sample Input / 範例輸入
5
12 8 15 0 5
Sample Output / 範例輸出
40
Hint / 提示:Read one amount on each repetition / 每次迴圈讀取一個金額。
ClassWork 3-24 · Sum of Input Squares / 輸入數值的平方總和
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Read N integers and calculate the sum of their squares. The values come from input; they are not necessarily 1 to N. 中文:讀入 N 個整數,把每個數字平方後加總;輸入數值不一定是 1 至 N。 |
| Input Format / 輸入格式 | Line 1: N. Line 2: N integers. 中文:第一行為 N,第二行為 N 個整數。 |
| Output Format / 輸出格式 | Print the integer sum of squares on one line. 中文:一行輸出平方總和。 |
| Constraints / 限制 | 1 ≤ N ≤ 100000; -1000000 ≤ each value ≤ 1000000; use long long for multiplication and the sum. |
| Difficulty / 難度 | Easy |
Sample Input / 範例輸入
4
-2 3 0 4
Sample Output / 範例輸出
29
Hint / 提示:Square the input value, not the loop counter / 平方的是讀入的數值,不是迴圈計數器。
ClassWork 3-25 · Count Excellent Scores / 計算高分人數
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Among N integer scores, count how many are at least 80. A score of exactly 80 is included. 中文:統計 N 個分數中不少於 80 分的人數,80 分也計算在內。 |
| Input Format / 輸入格式 | Line 1: N. Line 2: N integer scores. 中文:第一行為 N,第二行為 N 個整數分數。 |
| Output Format / 輸出格式 | Print the count on one line. 中文:一行輸出人數;沒有人符合時輸出 0。 |
| Constraints / 限制 | 1 ≤ N ≤ 1000; 0 ≤ score ≤ 100. |
| Difficulty / 難度 | Easy |
Sample Input / 範例輸入
6
79 80 100 60 85 0
Sample Output / 範例輸出
3
Hint / 提示:Increase the counter only when the score meets the condition / 符合條件時才增加計數。
ClassWork 3-26 · Rectangle of Hash Signs / 井號長方形
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Given R and C, print a rectangle with R rows and C hash signs (#) in every row. 中文:輸出 R 行,每行有 C 個井號 #。 |
| Input Format / 輸入格式 | Two integers in this order: R, then C. 中文:先輸入行數 R,再輸入每行的符號數 C。 |
| Output Format / 輸出格式 | Print R lines of C hash signs. Do not put spaces between signs or add blank rows. 中文:符號之間沒有空格,行與行之間沒有空白行。 |
| Constraints / 限制 | 1 ≤ R, C ≤ 20. |
| Difficulty / 難度 | Easy |
Sample Input / 範例輸入
2 4
Sample Output / 範例輸出
####
####
Hint / 提示:Finish one row before printing its newline / 完成一行的符號後才換行。
ClassWork 3-27 · Rectangle of One Number / 重複數字長方形
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Given R, C and K, print R rows, with C copies of K in each row. 中文:輸出 R 行,每行有 C 個相同的數字 K。 |
| Input Format / 輸入格式 | Three integers in this order: R, C, K. 中文:依次輸入行數 R、每行數量 C、數字 K。 |
| Output Format / 輸出格式 | Print R lines. Separate adjacent numbers with one space; use no leading or trailing spaces. 中文:相鄰數字以一個空格分隔,行首和行尾沒有空格。 |
| Constraints / 限制 | 1 ≤ R, C ≤ 20; 0 ≤ K ≤ 9. |
| Difficulty / 難度 | Easy |
Sample Input / 範例輸入
2 3 7
Sample Output / 範例輸出
7 7 7
7 7 7
Hint / 提示:Distinguish the number to print from the row and column counters / 分清輸出數字與行、列計數器。
ClassWork 3-28 · Even Number Triangle / 偶數三角形
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Given N, print N rows. Row i contains the first i positive even integers: 2, 4, …, 2i. 中文:輸出 N 行;第 i 行依次輸出前 i 個正偶數。 |
| Input Format / 輸入格式 | One integer N. 中文:輸入行數 N。 |
| Output Format / 輸出格式 | Print N lines, with one space between adjacent numbers and no leading or trailing spaces. 中文:數字之間以一個空格分隔,行首和行尾沒有空格。 |
| Constraints / 限制 | 1 ≤ N ≤ 9. |
| Difficulty / 難度 | Medium |
Sample Input / 範例輸入
4
Sample Output / 範例輸出
2
2 4
2 4 6
2 4 6 8
Hint / 提示:The number of values in a row depends on its row number / 每行的數字數量隨行號改變。
ClassWork 3-29 · Product of N Inputs / 輸入數值的乘積
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Read N positive integers and multiply all of them. 中文:讀入 N 個正整數,計算全部數字相乘的乘積。 |
| Input Format / 輸入格式 | Line 1: N. Line 2: N positive integers. 中文:第一行為 N,第二行為 N 個正整數。 |
| Output Format / 輸出格式 | Print the integer product on one line. 中文:一行輸出整數乘積。 |
| Constraints / 限制 | 1 ≤ N ≤ 15; 1 ≤ each value ≤ 9; use long long for the product. |
| Difficulty / 難度 | Easy |
Sample Input / 範例輸入
4
2 3 4 5
Sample Output / 範例輸出
120
Hint / 提示:Choose an initial product that does not make every result zero / 選擇不會令乘積一直為零的初值。
ClassWork 3-30 · Product of Even Values in a Range / 區間偶數連乘
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Given L and R, multiply every even integer in the inclusive range from L to R. 中文:把 L 至 R 之間的所有偶數相乘,包含兩端的偶數。 |
| Input Format / 輸入格式 | Two integers in this order: L, then R. 中文:先輸入 L,再輸入 R。 |
| Output Format / 輸出格式 | Print the integer product on one line. The constraints guarantee at least one even value in the range. 中文:一行輸出整數乘積;限制保證區間內至少有一個偶數。 |
| Constraints / 限制 | 1 ≤ L < R ≤ 20; use long long for the product. |
| Difficulty / 難度 | Medium |
Sample Input / 範例輸入
2 8
Sample Output / 範例輸出
384
Hint / 提示:Multiply only values that meet the even-number condition / 只把符合偶數條件的數字乘入。
ClassWork 3-31 · Odd Number Addition Expression / 奇數連加算式
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Given N, print the first N positive odd integers as an addition expression, followed by = and their calculated sum. 中文:把前 N 個正奇數寫成連加算式,最後輸出等號及實際總和。 |
| Input Format / 輸入格式 | One integer N, the number of terms. 中文:N 表示項數,不是數列的上限。 |
| Output Format / 輸出格式 | Print one line without spaces, such as 1+3+5+7=16. No + is allowed immediately before =. For N=1, print 1=1.中文:一行輸出,不留空格,等號前沒有多餘加號。 |
| Constraints / 限制 | 1 ≤ N ≤ 100. |
| Difficulty / 難度 | Medium |
Sample Input / 範例輸入
4
Sample Output / 範例輸出
1+3+5+7=16
Hint / 提示:Work out the odd value of each term before printing and adding it / 先求每一項的奇數,再輸出及累加。
ClassWork 3-32 · Even Number Addition Expression / 偶數連加算式
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Given N, print the first N positive even integers as an addition expression, followed by = and their calculated sum. 中文:把前 N 個正偶數寫成連加算式,最後輸出等號及實際總和。 |
| Input Format / 輸入格式 | One integer N, the number of terms. 中文:N 表示項數,不是數列的上限。 |
| Output Format / 輸出格式 | Print one line without spaces, such as 2+4+6+8=20. No + is allowed immediately before =. For N=1, print 2=2.中文:一行輸出,不留空格,等號前沒有多餘加號。 |
| Constraints / 限制 | 1 ≤ N ≤ 100. |
| Difficulty / 難度 | Medium |
Sample Input / 範例輸入
4
Sample Output / 範例輸出
2+4+6+8=20
Hint / 提示:Print the equals sign and final sum after the repeated output / 重複輸出各項後,才輸出等號及最後總和。
Before Submission / 交功課前自查
- Did I use the required ClassWork 3-x number? 中文:題號是否正確?
- Does every
forloop use{ }, even for one statement? 中文:每個 for 是否都有大括號,沒有單行省略寫法? - Does every C++
if,else ifandelsebranch use{ }? 中文:每個條件分支是否都有大括號? - Is the loop start, condition and update correct? 中文:起點、條件及更新是否一致?
- Did I initialise sums to 0 and products to 1 outside the loop? 中文:累加、累乘初值是否正確?
- Does the output match spaces, lines and decimal places? 中文:空格、換行及小數位是否符合要求?
- Have I tested one repetition, both endpoints and no matching values where allowed? 中文:有否測試一次迴圈、邊界及無符合結果?