Unit 3 · For Loops and Conditions
Unit 3 · For Loops and Conditions
Last updated / 更新日期:2026-10-06
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. 中文:執行範例,也測試最小輸入。
Unit Dashboard / 單元地圖
| Learn | Use in C++ | Try |
|---|---|---|
| Repeat a known number of times / 重複指定次數 | for | Example 3-1, 3-4; ClassWork 3-10, 3-11 |
| Change the step or direction / 步幅及倒數 | i += 2, i-- | Example 3-5; ClassWork 3-12, 3-13 |
| Accumulate and count / 累加及計數 | sum += i, count++ | Example 3-2, 3-3, 3-6; ClassWork 3-1 to 3-5 |
| Read repeated input / 重複讀取資料 | cin inside a loop | Example 3-7, 3-8; ClassWork 3-15 to 3-17 |
| Combine conditions / 組合條件 | &&, || | ClassWork 3-3, 3-4, 3-19 |
| Build rows and columns / 行與列 | nested for loops | Example 3-9, 3-10; ClassWork 3-8, 3-9, 3-20 |
C++ For Loop Syntax / for 迴圈格式
Use a for loop when the number of repetitions or the range is known.
中文:知道重複次數,或知道要走訪的數字範圍時,可使用 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.
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 | Header | 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; | Add the current value / 累加本次數值 |
long long product = 1; | Multiplication starts at 1 / 累乘初值為 1 |
count++; | Count one match / 每符合一次便加一 |
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。
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 標記。沒有符合數字時,數列那一行仍須輸出換行。
Nested For Loops / 巢狀迴圈
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 / 修正 |
|---|---|
for (int i = 1; i <= n; i++); | Remove the final semicolon; it creates an empty 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 Python (if…else) / Python 的士收費題目
Write a program in Python to calculate and display the taxi fare using if...else.
中文:使用 Python 的 if...else 編寫程式,輸入行駛里程(米),計算並輸出的士車資。
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | 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 / Python 參考答案
distance = int(input())
if distance <= 1500:
fare = 20
else:
extra_distance = distance - 1500
blocks = (extra_distance + 499) // 500
fare = 20 + blocks * 2
print(f"MOP {fare}")
How It Works / 計算說明
if distance <= 1500: use the base fare of 20. 中文:未超過 1500 米,收基本車資 20。else: calculate only the distance beyond 1500 metres. 中文:超過起步里程後,只計算超出的部分。(extra_distance + 499) // 500: round up the number of 500-metre blocks for integer mileage. 中文://是整數除法;先加 499,令不足 500 米的部分亦計作一段。print(f"MOP {fare}"): display the currency and 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 numbers from 1 to 100, then print numbers from 1 to 100 with a step of 3. 中文:輸出兩個指定數列。 |
| 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
Hint / 提示:Use two for loops; do not leave a trailing space.
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
Hint / 提示:Use an accumulator in a loop.
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 L, R and 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
Hint / 提示:Use i % K == 0.
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
Hint / 提示:Initialise at 1 and include N / 從 1 開始,包含 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
Hint / 提示:Use i– / 每次減一。
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. |
| Output Format / 輸出格式 | Print the sum. |
| Constraints / 限制 | 1 ≤ N ≤ 100000 |
Sample Input / 範例輸入
10
Sample Output / 範例輸出
30
Hint / 提示:Step by 2; for N=1 the sum stays 0 / 每次加 2。
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 / 輸入格式 | N, followed by N integers. |
| 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
Hint / 提示:Read inside the loop / cin 放在迴圈內。
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 / 題目說明 | Count scores of at least 50. 中文:計算不少於 50 分的人數。 |
| Input Format / 輸入格式 | N, followed by N integer scores. |
| Output Format / 輸出格式 | Print the passing count. |
| Constraints / 限制 | 1 ≤ N ≤ 1000; 0 ≤ score ≤ 100 |
Sample Input / 範例輸入
5
49 50 75 0 100
Sample Output / 範例輸出
3
Hint / 提示:Count only inside the matching if / 合格才加一。
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 R and C. |
| Output Format / 輸出格式 | R lines; no spaces between stars. |
| Constraints / 限制 | 1 ≤ R, C ≤ 20 |
Sample Input / 範例輸入
2 4
Sample Output / 範例輸出
****
****
Hint / 提示:Outer loop for rows, inner for columns / 外層行,內層列。
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 / 題目說明 | Row i contains the numbers 1 to 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
Hint / 提示:Inner bound changes with row / 內層上限跟隨行數。
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;
}
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. 中文:交錯加減平方。 |
| 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: weight and height. 中文:兩個實數:體重、身高。 |
| 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 and iomanip. Supplementary condition/arithmetic revision / 此題為條件及算術補充複習。
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 one line without a + after the last number.中文:最後不可有加號。 |
| 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 / 輸出格式 | Each row contains products separated by one space. 中文:每列以一個空格分隔。 |
| 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 / 輸出格式 | Row i contains exactly i stars. 中文:第 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!. |
| 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 L and 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 L and 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. |
| 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 / 輸入格式 | N, followed by N integers. |
| 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 / 輸入格式 | N, followed by N integer scores. |
| Output Format / 輸出格式 | Line 1: passing count. Line 2: sum of passing scores. |
| 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 / 輸入格式 | N, followed by N integers. |
| Output Format / 輸出格式 | Print the maximum. |
| Constraints / 限制 | 1 ≤ N ≤ 1000; -1000000 ≤ value ≤ 1000000 |
Sample Input / 範例輸入
4
-8 -2 -5 -11
Sample Output / 範例輸出
-2
Hint / 提示:Initialise from the first input, not 0 / 用第一個輸入初始化,勿設為 0。
ClassWork 3-18 · Divisor List / 因數列表
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | List all positive divisors of N and count them. 中文:列出 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 其中一個整除的數。 |
| 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 / 每行減少內層次數。
Before Submission / 交功課前自查
- Did I use the required ClassWork 3-x number? 中文:題號是否正確?
- 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? 中文:有否測試一次迴圈、邊界及無符合結果?