Unit 5 · While Loop

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
whilerepeat while a condition is trueunknown repetitions
% 10get the last digitdigit problems
/ 10remove the last digitreverse and count
nested whileloop inside a looptables and patterns

Quick Concepts / 重點

SyntaxStudent note
while (n > 0)The loop variable must change. 中文:避免無限迴圈。
digit = n % 10;Last digit only.
n /= 10;Remove last digit.
original = n;Save the value before changing it.

From the 2022/23 Notebook / 補充

int reversed = 0;
while (n > 0) { reversed = reversed * 10 + n % 10; n /= 10; }
Extra taskInputOutput
Even Digits, right to left1234567898 6 4 2
Multiples of 3 with while10count 3, sum 18
3 × 3 while table31 2 3 / 2 4 6 / 3 6 9

C++ Syntax / C++ 語法

This complete program reverses a positive integer. 中文:可直接執行,反轉正整數。

#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;
    int reversed = 0;
    while (n > 0) {
        reversed = reversed * 10 + n % 10;
        n /= 10;
    }
    cout << reversed << endl;
    return 0;
}

Yellow Syntax Guide / 黃色語法重點

SyntaxMeaning
while (n > 0)keep working while n has digits
n % 10get the last digit
n /= 10remove the last digit
reversed = reversed * 10 + digitbuild the reversed number

Dry Run / 手算追蹤

For 4729: reversed becomes 9 → 92 → 927 → 9274. 中文:每次取最右邊一位,加入新數字的右方。

Examples / 例題

Example 5-1 · Count Digits

題目說明 / Problem Statement

Given a positive integer N, count its decimal digits. 中文:計算整數位數。

輸入格式 / Input Format

One integer N. 中文:一個整數 N。

輸出格式 / Output Format

Print the digit count. 中文:輸出位數。

限制 / Constraints

1 ≤ N ≤ 10^9

範例輸入 / Sample Input

59371

範例輸出 / Sample Output

5

Hint / 提示:Repeatedly divide N by 10.

Reference Answer / 參考答案

#include <iostream>
using namespace std;

int main() {
    int n, count = 0;
    cin >> n;
    while (n > 0) {
        count++;
        n /= 10;
    }
    cout << count << '\n';
    return 0;
}

Example 5-2 · Reverse Number

題目說明 / Problem Statement

Given a positive integer N, print its digits in reverse order. 中文:反轉整數。

輸入格式 / Input Format

One integer N. 中文:一個整數 N。

輸出格式 / Output Format

Print the reversed integer. 中文:輸出反轉結果。

限制 / Constraints

1 ≤ N ≤ 10^9

範例輸入 / Sample Input

4729

範例輸出 / Sample Output

9274

Hint / 提示:Take N % 10, then divide N by 10.

Reference Answer / 參考答案

#include <iostream>
using namespace std;

int main() {
    int n, reversed = 0;
    cin >> n;
    while (n > 0) {
        reversed = reversed * 10 + n % 10;
        n /= 10;
    }
    cout << reversed << '\n';
    return 0;
}

Example 5-3 · Decimal to Binary

題目說明 / Problem Statement

Convert a positive decimal integer to binary. 中文:十進位轉二進位。

輸入格式 / Input Format

One integer N. 中文:一個整數 N。

輸出格式 / Output Format

Print its binary representation. 中文:輸出二進位表示。

限制 / Constraints

1 ≤ N ≤ 10^6

範例輸入 / Sample Input

13

範例輸出 / Sample Output

1101

Hint / 提示:Collect remainders and print them backwards.

Reference Answer / 參考答案

#include <iostream>
#include <string>
using namespace std;

int main() {
    int n;
    cin >> n;
    string binary;
    while (n > 0) {
        binary = char('0' + n % 2) + binary;
        n /= 2;
    }
    cout << binary << '\n';
    return 0;
}

ClassWork / 課後練習

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

ClassWork 9-1 · Largest Digit

題目說明 / Problem Statement

Given N, find its largest decimal digit. 中文:找出最大的數字位。

輸入格式 / Input Format

One integer N. 中文:一個整數 N。

輸出格式 / Output Format

Print the largest digit. 中文:輸出最大位數字。

限制 / Constraints

1 ≤ N ≤ 10^9

範例輸入 / Sample Input

59371

範例輸出 / Sample Output

9

Hint / 提示:Compare every N % 10.

ClassWork 9-2 · Palindrome Number

題目說明 / Problem Statement

Determine whether a positive integer reads the same forwards and backwards. 中文:判斷回文數。

輸入格式 / Input Format

One integer N. 中文:一個整數 N。

輸出格式 / Output Format

Print Palindrome or Not Palindrome. 中文:輸出判斷結果。

限制 / Constraints

1 ≤ N ≤ 10^9

範例輸入 / Sample Input

1331

範例輸出 / Sample Output

Palindrome

Hint / 提示:Reverse a copy of the original number.

ClassWork 10-1 · Inverted Stars

題目說明 / Problem Statement

Given N, print an inverted right triangle of stars. 中文:輸出倒三角星號。

輸入格式 / Input Format

One integer N. 中文:一個整數 N。

輸出格式 / Output Format

First row has N stars; last row has 1 star. 中文:首列 N 個星號,末列 1 個。

限制 / Constraints

1 ≤ N ≤ 50

範例輸入 / Sample Input

4

範例輸出 / Sample Output


*

*

Hint / 提示:Use nested while loops only.

ClassWork 10-2 · Bubble Sort

題目說明 / Problem Statement

Read N integers and sort them in ascending order using nested while loops only. 中文:只用雙層 while 實作氣泡排序。

輸入格式 / Input Format

Line 1: N. Line 2: N integers. 中文:第一行 N;第二行 N 個整數。

輸出格式 / Output Format

Print sorted values separated by spaces. 中文:以空格輸出排序結果。

限制 / Constraints

1 ≤ N ≤ 100; no for loop or library sort.

範例輸入 / Sample Input

5
4 2 9 1 6

範例輸出 / Sample Output

1 2 4 6 9

Hint / 提示:Swap adjacent values when they are in the wrong order.

Built with LogoFlowershow