Unit 3 · Loops and Conditions

Start Here / 開始做題

  1. Read Input and Output first. 中文:先找輸入及輸出。
  2. Write down the variables you need. 中文:先決定變數。
  3. Trace the sample once by hand. 中文:用範例手算一次。
  4. Write, run, then compare every output character. 中文:執行後逐字比較輸出。

Unit Dashboard / 單元地圖

LearnMeaningUse
forrepeat a known number of timessequences and totals
if...elsechoose one resultgrades and fares
&& / ``
nested loopsrepeat rows and columnstables and patterns

Quick Concepts / 重點

SyntaxStudent note
for (int i = 1; i <= n; i++)Start, condition, update. 中文:開始、檢查、更新。
sum += i;Add to a running total. 中文:累加。
count++;Count one matching value. 中文:符合才計數。
if (condition)Check the most specific condition first. 中文:嚴格條件放前面。

From the 2022/23 Notebook / 補充

// Taxi fare: first 2000 m costs 19; every started 500 m after costs 2.
int fare = 19;
if (distance > 2000) fare += ((distance - 2000 + 499) / 500) * 2;
Extra taskInputOutput
Taxi Fare250021
Multiples of 310values 3 6 9, count 3, sum 18
Score Grade84B

C++ Syntax / C++ 語法

This is a complete program. You can copy, run and then change L, R and k. 中文:以下是可直接執行的完整程式。

#include <iostream>
using namespace std;

int main() {
    int L, R, k;
    cin >> L >> R >> k;
    long long sum = 0;
    for (int i = L; i <= R; i++) {
        if (i % k == 0) sum += i;
    }
    cout << sum << endl;
    return 0;
}

Yellow Syntax Guide / 黃色語法重點

SyntaxMeaning
for (int i = L; i <= R; i++)repeat through a range
if (i % k == 0)test whether i is divisible by k
sum += iadd i to the running total
cout << sum << endl;print the final answer

Dry Run / 手算追蹤

For L=1, R=10, k=3:

ii % 3 == 0sum
1no0
3yes3
6yes9
9yes18

Examples / 例題

Example 3-1 · Two Sequences

題目說明 / 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

題目說明 / 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

題目說明 / 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;
}

ClassWork / 課後練習

Complete every task independently. Submit source code and a screenshot of a successful run. 中文:獨立完成,提交程式碼及成功輸出截圖。

ClassWork 3-1 · List Multiples of 7

題目說明 / 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: multiples separated by spaces. 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

題目說明 / 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 · Common Divisors

題目說明 / 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

Use the same three-line format as ClassWork 3-1. 中文:格式同 ClassWork 3-1。

限制 / Constraints

1 ≤ L ≤ R ≤ 10^5; A, B ≥ 1

範例輸入 / Sample Input

1 100 5 11

範例輸出 / Sample Output

55
1
55

Hint / 提示:Use logical AND: i % A == 0 && i % B == 0.

ClassWork 4-1 · Divisible by A or B

題目說明 / 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

Print values, count and sum on three lines. 中文:三行輸出數列、數量及總和。

限制 / Constraints

1 ≤ L ≤ R ≤ 10^5

範例輸入 / Sample Input

1 30 13 29

範例輸出 / Sample Output

13 26 29
3
68

Hint / 提示:Use logical OR: ||.

ClassWork 4-2 · Alternating Squares

題目說明 / 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 5-1 · BMI

題目說明 / 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.

ClassWork 5-2 · Addition Expression

題目說明 / 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 6-1 · Multiplication Table

題目說明 / 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 6-2 · Right Triangle

題目說明 / 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.

Built with LogoFlowershow