Unit 1 · Computer Basics and C++ Foundation

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 / 單元地圖

LearnUse in C++Try
How a computer handles datainput → process → output → storageExample 1-1 to 1-4
How data is measuredbit, byte, KB, MB, GBClassWork 1-1
How a C++ program worksmain, cin, cout, variablesClassWork 1-2 to 1-4
How to use data and operatorstypes, arithmetic, comparison, logicExample 1-2 to 1-12; ClassWork 1-3 to 1-16
How to make decisions and repeat%, if...else, forClassWork 1-5, 1-12 to 1-16

Computer Basics / 計算機概論

IdeaShort explanation
InputData enters the computer: keyboard, mouse, sensor. 中文:輸入資料。
ProcessThe CPU follows instructions to transform data. 中文:按指令處理資料。
OutputThe result is shown, played or saved. 中文:輸出結果。
StorageData is kept in memory or files. 中文:儲存資料。

Storage Units / 儲存單位

UnitMeaning
bitOne binary digit: 0 or 1. 中文:最小資料單位。
byte8 bits. Often enough for one ASCII character. 中文:8 個 bit。
KB / MB / GB1024 bytes / 1024 KB / 1024 MB in this course.
Character encodingEnglish letters often use 1 byte; Chinese UTF-8 characters commonly use 3 bytes. 中文:實際大小取決於編碼。

C++ Starter Kit / C++ 入門

PartPurpose
#include <iostream>Enables standard input and output.
using namespace std;Lets us write cout and cin directly.
int main()Program entry point. 中文:程式由這裡開始。
cin >> value;Reads input.
cout << value;Prints output.
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    int a, b;
    cin >> a >> b;
    cout << a + b << '\n';
    return 0;
}

Yellow Syntax Guide / 黃色語法重點

SyntaxMeaning
#include <iostream>allows cin and cout
int main()program starts here
cin >> variableread input
cout << valueprint output
return 0;program ends normally

Data Types and Operators / 資料型別及運算子

ItemUseExample
intwhole number / 整數int age = 15;
long longlarge whole number / 大整數long long bytes;
doubledecimal number / 小數double average;
stringone word or text / 文字string name;
+ - * / %arithmetic operators / 運算子17 % 2 is 1
== != < > <= >=comparison / 比較score >= 50
`&&!`
`&^ ~`bitwise operators / 位元運算子

Operator Guide / 運算子完整導覽

GroupOperatorsMeaning / 中文F3 note
Assignment=store a value / 儲存數值= is not “equal to”.
Arithmetic+ - * / %add, subtract, multiply, divide, remainder / 加減乘除、餘數% works with integers.
Integer divisiona / bquotient without decimal when both are int / 整數除法7 / 2 is 3; use 7.0 / 2 for 3.5.
Unary sign+x, -xpositive or negative value / 正、負號-x does not change x.
Update by one++x, x++, --x, x--increase/decrease by 1 / 加一、減一Prefer one per statement at this stage.
Compound assignment+= -= *= /= %=calculate then store / 計算後存回sum += x means sum = sum + x.
Comparison== != < > <= >=compare two values / 比較數值The result is true or false.
Logical==`&&!`==
Conditionalcondition ? A : Bchoose one of two values / 二選一A short if...else expression.

| Bitwise / 位元 | & | ^ ~ | AND, OR, XOR, NOT on binary bits / 對二進位位元運算 | Use non-negative integers first. |

Bitwise Operators / 位元運算子

Computers store integers as binary bits. A bitwise operator compares or changes each bit position directly. 中文:位元運算子直接處理二進位的每一位。

OperatorRule / 中文4-bit example
a & bAND: both bits are 1 → 1 / 兩位皆為 1 才是 11100 & 1010 = 1000
==`ab`==OR: either bit is 1 → 1 / 有一位為 1 就是 1
a ^ bXOR: different bits → 1 / 不同為 1,相同為 01100 ^ 1010 = 0110
~aNOT: reverse every bit / 每一位反轉for 8 bits: ~00001101 = 11110010

Do Not Mix These / 不要混淆

SymbolUseExample
&&logical AND: combine true/false conditionsage >= 12 && age <= 18
&bitwise AND: compare binary bits12 & 10 gives 8
==``==
==``==bitwise OR: combine binary bits

Bitwise Dry Run / 位元手算

For a = 12 (1100₂) and b = 10 (1010₂):

OperationBinary resultDecimal result
a & b1000₂8
`ab`1110₂
a ^ b0110₂6

Bit Masks / 位元遮罩

Count positions from the right: bit 0 is the rightmost bit. A mask has one 1 at the position to inspect or change. 中文:由右邊開始數,最右邊是 bit 0;遮罩只在目標位置放一個 1

PositionBinary maskDecimal mask
bit 00001₂1
bit 10010₂2
bit 20100₂4
bit 31000₂8

For ~, always state the width. This unit uses 8 bits in the NOT example so the output is predictable.

Operator Order / 運算次序

PriorityDo firstExample
1parentheses / 括號(a + b) * c
2unary -, !, ++, ---x
3*, /, %a + b * c
4+, -a - b + c
5comparisonsscore >= 50
6&&, then `

Common errors / 常見錯誤:write x == 5 to compare, but x = 5 to assign. Never divide by zero. For a decimal result, make at least one operand double.

Dry Run / 手算追蹤

For input 8 3, calculate 8+3=11, 8-3=5, 8*3=24 before coding. 中文:先手算範例,再寫程式。

Read This Before Coding / 學生自查

  • Does every variable have a suitable type?
  • Does the program read exactly the stated input?
  • Does the output match the sample, including decimal places and line breaks?

Examples / 例題

Example 1-1 · Hello, F3!

ItemDetails
Problem Statement / 題目說明Print the exact text Hello, F3! on one line.
中文:輸出指定文字。
Input Format / 輸入格式No standard input.
中文:不需要輸入。
Output Format / 輸出格式Print one line exactly as required.
中文:完全依指定輸出一行。
Constraints / 限制No input.

Sample Input / 範例輸入

(no standard input)

Sample Output / 範例輸出

Hello, F3!

C++ focus / 語法重點:Use cout << "text" << '\n';.

Reference Answer / 參考答案

#include <iostream>
using namespace std;

int main() {
    cout << "Hello, F3!\n";
    return 0;
}

Example 1-2 · Simple Calculator

ItemDetails
Problem Statement / 題目說明Given two integers A and B, print their sum, difference and product on separate lines.
中文:計算兩個整數的加、減、乘。
Input Format / 輸入格式Two integers A and B.
中文:兩個整數 A、B。
Output Format / 輸出格式Print A+B, A-B and A×B, one result per line.
中文:每行輸出一個結果。
Constraints / 限制-10^6 ≤ A, B ≤ 10^6

Sample Input / 範例輸入

8 3

Sample Output / 範例輸出

11
5
24

C++ focus / 語法重點:Declare int a, b; and read with cin >> a >> b;.

Reference Answer / 參考答案

#include <iostream>
using namespace std;

int main() {
    int a, b;
    cin >> a >> b;
    cout << a + b << '\n';
    cout << a - b << '\n';
    cout << a * b << '\n';
    return 0;
}

Example 1-3 · Average with Decimals

ItemDetails
Problem Statement / 題目說明Given three integer marks, print their average with exactly 2 decimal places.
中文:計算三個分數的平均值。
Input Format / 輸入格式Three integers A, B and C.
中文:三個整數。
Output Format / 輸出格式Print the average with 2 digits after the decimal point.
中文:固定輸出兩位小數。
Constraints / 限制0 ≤ A, B, C ≤ 100

Sample Input / 範例輸入

80 75 90

Sample Output / 範例輸出

81.67

C++ focus / 語法重點:Use double average = (a + b + c) / 3.0; and fixed << setprecision(2).

Reference Answer / 參考答案

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

int main() {
    int a, b, c;
    cin >> a >> b >> c;
    cout << fixed << setprecision(2) << (a + b + c) / 3.0 << '\n';
    return 0;
}

Example 1-4 · Odd or Even

ItemDetails
Problem Statement / 題目說明Given an integer N, print Even when it is divisible by 2; otherwise print Odd.
中文:判斷奇偶。
Input Format / 輸入格式One integer N.
中文:一個整數 N。
Output Format / 輸出格式Print Even or Odd.
中文:輸出 Even 或 Odd。
Constraints / 限制-10^9 ≤ N ≤ 10^9

Sample Input / 範例輸入

17

Sample Output / 範例輸出

Odd

C++ focus / 語法重點:Use % and if...else.

Reference Answer / 參考答案

#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;
    if (n % 2 == 0) cout << "Even\n";
    else cout << "Odd\n";
    return 0;
}

Example 1-5 · Quotient and Remainder

ItemDetails
Problem Statement / 題目說明Given two positive integers A and B, print the integer quotient and the remainder.
中文:輸出整數商及餘數。
Input Format / 輸入格式Two positive integers A and B.
中文:兩個正整數。
Output Format / 輸出格式Line 1: A divided by B. Line 2: A modulo B.
中文:第一行商,第二行餘數。
Constraints / 限制1 ≤ A ≤ 10^9; 1 ≤ B ≤ 10^6

Sample Input / 範例輸入

17 5

Sample Output / 範例輸出

3
2

C++ focus / 語法重點:Use / for quotient and % for remainder.

Reference Answer / 參考答案

#include <iostream>
using namespace std;

int main() {
    int a, b;
    cin >> a >> b;
    cout << a / b << endl;
    cout << a % b << endl;
    return 0;
}

Example 1-6 · Minutes and Seconds

ItemDetails
Problem Statement / 題目說明Given a number of seconds S, convert it into whole minutes and remaining seconds.
中文:把秒數轉為分鐘及剩餘秒數。
Input Format / 輸入格式One integer S.
中文:一個整數 S。
Output Format / 輸出格式Print minutes seconds on one line.
中文:同一行輸出分鐘及秒數。
Constraints / 限制0 ≤ S ≤ 10^6

Sample Input / 範例輸入

367

Sample Output / 範例輸出

6 7

C++ focus / 語法重點:Use / and % with 60.

Reference Answer / 參考答案

#include <iostream>
using namespace std;

int main() {
    int s;
    cin >> s;
    cout << s / 60 << ' ' << s % 60 << endl;
    return 0;
}

Example 1-7 · Celsius to Fahrenheit

ItemDetails
Problem Statement / 題目說明Given a Celsius temperature C, print Fahrenheit F = C × 9 / 5 + 32 with 1 decimal place.
中文:將攝氏轉為華氏。
Input Format / 輸入格式One real number C.
中文:一個實數 C。
Output Format / 輸出格式Print F with exactly 1 digit after the decimal point.
中文:固定輸出一位小數。
Constraints / 限制-100 ≤ C ≤ 100

Sample Input / 範例輸入

25

Sample Output / 範例輸出

77.0

C++ focus / 語法重點:Use double; write 9.0 / 5 to avoid integer division.

Reference Answer / 參考答案

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

int main() {
    double c;
    cin >> c;
    cout << fixed << setprecision(1) << c * 9.0 / 5 + 32 << endl;
    return 0;
}

Example 1-8 · Update a Score

ItemDetails
Problem Statement / 題目說明A player starts with score S. They gain A points and lose B points. Print the final score.
中文:計算加分、扣分後的分數。
Input Format / 輸入格式Three integers S, A and B.
中文:三個整數。
Output Format / 輸出格式Print the final score.
中文:輸出最後分數。
Constraints / 限制0 ≤ S, A, B ≤ 10^6

Sample Input / 範例輸入

50 12 7

Sample Output / 範例輸出

55

C++ focus / 語法重點:Use += and -= to update a variable.

Reference Answer / 參考答案

#include <iostream>
using namespace std;

int main() {
    int score, add, lose;
    cin >> score >> add >> lose;
    score += add;
    score -= lose;
    cout << score << endl;
    return 0;
}

Example 1-9 · Next Number

ItemDetails
Problem Statement / 題目說明Given an integer N, print N and the next integer on separate lines.
中文:輸出原數及下一個整數。
Input Format / 輸入格式One integer N.
中文:一個整數 N。
Output Format / 輸出格式Line 1: N. Line 2: N + 1.
中文:第一行原數,第二行下一個數。
Constraints / 限制-10^9 ≤ N ≤ 10^9

Sample Input / 範例輸入

99

Sample Output / 範例輸出

99
100

C++ focus / 語法重點:Use n++ after printing. It increases n by one.

Reference Answer / 參考答案

#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;
    cout << n << endl;
    n++;
    cout << n << endl;
    return 0;
}

Example 1-10 · Larger Number

ItemDetails
Problem Statement / 題目說明Given two integers A and B, print the larger value. If they are equal, print either value.
中文:輸出較大的數。
Input Format / 輸入格式Two integers A and B.
中文:兩個整數。
Output Format / 輸出格式Print one integer.
中文:輸出一個整數。
Constraints / 限制-10^9 ≤ A, B ≤ 10^9

Sample Input / 範例輸入

8 13

Sample Output / 範例輸出

13

C++ focus / 語法重點:Use a > b ? a : b as a short two-choice expression.

Reference Answer / 參考答案

#include <iostream>
using namespace std;

int main() {
    int a, b;
    cin >> a >> b;
    cout << (a > b ? a : b) << endl;
    return 0;
}

Example 1-11 · Number in Range

ItemDetails
Problem Statement / 題目說明Given N, print YES when 10 ≤ N ≤ 99; otherwise print NO.
中文:判斷數字是否在 10 至 99 的範圍內。
Input Format / 輸入格式One integer N.
中文:一個整數 N。
Output Format / 輸出格式Print YES or NO.
中文:輸出 YES 或 NO。
Constraints / 限制-10^9 ≤ N ≤ 10^9

Sample Input / 範例輸入

42

Sample Output / 範例輸出

YES

C++ focus / 語法重點:Use && when both comparisons must be true.

Reference Answer / 參考答案

#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;
    if (n >= 10 && n <= 99) cout << "YES" << endl;
    else cout << "NO" << endl;
    return 0;
}

Example 1-12 · Access Check

ItemDetails
Problem Statement / 題目說明Given age A and a membership flag M (0 or 1), print Allow if A is at least 12 or M is 1; otherwise print Deny.
中文:年齡達 12 歲或是會員即可進入。
Input Format / 輸入格式Two integers A and M.
中文:年齡及會員標記。
Output Format / 輸出格式Print Allow or Deny.
中文:輸出 Allow 或 Deny。
Constraints / 限制0 ≤ A ≤ 120; M is 0 or 1

Sample Input / 範例輸入

10 1

Sample Output / 範例輸出

Allow

C++ focus / 語法重點:Use || when either condition may be true.

Reference Answer / 參考答案

#include <iostream>
using namespace std;

int main() {
    int age, member;
    cin >> age >> member;
    if (age >= 12 || member == 1) cout << "Allow" << endl;
    else cout << "Deny" << endl;
    return 0;
}

Example 1-13 · Bitwise AND, OR and XOR

ItemDetails
Problem Statement / 題目說明Given two non-negative integers A and B, print A AND B, A OR B, and A XOR B on separate lines.
中文:依序輸出兩數的位元 AND、OR、XOR 結果。
Input Format / 輸入格式Two non-negative integers A and B.
中文:兩個非負整數。
Output Format / 輸出格式Print A & B, `A
Constraints / 限制0 ≤ A, B ≤ 255

Sample Input / 範例輸入

12 10

Sample Output / 範例輸出

8
14
6

C++ focus / 語法重點:Use &, |, and ^. These are not logical && or ||.

Reference Answer / 參考答案

#include <iostream>
using namespace std;

int main() {
    int a, b;
    cin >> a >> b;
    cout << (a & b) << endl;
    cout << (a | b) << endl;
    cout << (a ^ b) << endl;
    return 0;
}

Example 1-14 · Eight-Bit NOT

ItemDetails
Problem Statement / 題目說明Given an integer N from 0 to 255, print its 8-bit bitwise NOT result.
中文:輸出 N 的 8 位元 NOT 結果。
Input Format / 輸入格式One integer N.
中文:一個整數 N。
Output Format / 輸出格式Print exactly 8 binary digits.
中文:剛好輸出 8 位二進位。
Constraints / 限制0 ≤ N ≤ 255

Sample Input / 範例輸入

13

Sample Output / 範例輸出

11110010

C++ focus / 語法重點:~n flips all bits. & 255 keeps only the last 8 bits. bitset<8> prints eight binary digits.

Reference Answer / 參考答案

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

int main() {
    int n;
    cin >> n;
    cout << bitset<8>((~n) & 255) << endl;
    return 0;
}

ClassWork / 課後練習

Complete each task independently. Submit your C++ source file and one screenshot of successful output. 中文:獨立完成,提交程式碼及成功輸出截圖。

ClassWork 1-1 · Storage Estimate

ItemDetails
Problem Statement / 題目說明Assume 1 GB = 1024³ bytes and one Chinese character uses 3 bytes in UTF-8. Given G GB, calculate the maximum whole number of characters that can be stored.
中文:以 UTF-8 估算可儲存中文字數。
Input Format / 輸入格式One integer G.
中文:一個整數 G(GB)。
Output Format / 輸出格式Print the maximum whole number of characters.
中文:輸出整數字元數。
Constraints / 限制1 ≤ G ≤ 100; use long long.

Sample Input / 範例輸入

1

Sample Output / 範例輸出

357913941

C++ focus / 語法重點:Use integer division with long long. Encoding affects real storage size.

ClassWork 1-2 · Student Profile

ItemDetails
Problem Statement / 題目說明Read a student number, a first name (one word), and an age. Print them in the required three-line format.
中文:讀取並格式化輸出學生資料。
Input Format / 輸入格式Line 1: integer ID. Line 2: one-word name. Line 3: integer age.
中文:依序輸入編號、姓名、年齡。
Output Format / 輸出格式Print ID:, Name: and Age: on separate lines.
中文:依格式輸出三行。
Constraints / 限制1 ≤ ID ≤ 999999; 1 ≤ age ≤ 120

Sample Input / 範例輸入

23001
Wing
15

Sample Output / 範例輸出

ID: 23001
Name: Wing
Age: 15

C++ focus / 語法重點:Use int, string, and cin.

ClassWork 1-3 · Division with Precision

ItemDetails
Problem Statement / 題目說明Given integers X and Y, print X divided by Y with exactly 2 decimal places.
中文:固定輸出兩位小數。
Input Format / 輸入格式Two integers X and Y.
中文:兩個整數 X、Y。
Output Format / 輸出格式Print the quotient with 2 digits after the decimal point.
中文:輸出兩位小數商。
Constraints / 限制

Sample Input / 範例輸入

100 3

Sample Output / 範例輸出

33.33

C++ focus / 語法重點:Convert one operand to double or divide by 3.0.

ClassWork 1-4 · Rectangle Calculator

ItemDetails
Problem Statement / 題目說明Given the length L and width W of a rectangle, print its perimeter and area.
中文:計算長方形周界及面積。
Input Format / 輸入格式Two integers L and W.
中文:兩個整數 L、W。
Output Format / 輸出格式Line 1: perimeter. Line 2: area.
中文:第一行周界,第二行面積。
Constraints / 限制1 ≤ L, W ≤ 10^5

Sample Input / 範例輸入

8 3

Sample Output / 範例輸出

22
24

C++ focus / 語法重點:Perimeter is 2 * (L + W); area is L * W.

ClassWork 1-5 · Even Sum

ItemDetails
Problem Statement / 題目說明Given A and B where A ≤ B, calculate the sum of all even integers in the inclusive range [A, B].
中文:計算區間內所有偶數總和。
Input Format / 輸入格式Two integers A and B.
中文:兩個整數 A、B。
Output Format / 輸出格式Print one integer: the required sum.
中文:輸出總和。
Constraints / 限制1 ≤ A ≤ B ≤ 100000

Sample Input / 範例輸入

3 10

Sample Output / 範例輸出

28

C++ focus / 語法重點:Use a for loop and test i % 2 == 0.

ClassWork 1-6 · Four Operations

ItemDetails
Problem Statement / 題目說明Given two integers A and B, print A+B, A-B, A×B and A÷B (integer quotient), each on a separate line.
中文:依序輸出加、減、乘、整數商。
Input Format / 輸入格式Two integers A and B.
中文:兩個整數。
Output Format / 輸出格式Print four integers, one per line.
中文:每行輸出一個結果。
Constraints / 限制-10^6 ≤ A ≤ 10^6; 1 ≤ B ≤ 10^6

Sample Input / 範例輸入

20 6

Sample Output / 範例輸出

26
14
120
3

C++ focus / 語法重點:Use the four arithmetic operators. Keep both variables as int.

ClassWork 1-7 · School Bell Time

ItemDetails
Problem Statement / 題目說明Given an elapsed time S in seconds, print the number of whole minutes and remaining seconds in the format M minutes S seconds.
中文:把秒數轉換成分鐘及秒數。
Input Format / 輸入格式One integer S.
中文:一個整數。
Output Format / 輸出格式Print exactly the required format.
中文:完全依指定格式輸出。
Constraints / 限制0 ≤ S ≤ 36000

Sample Input / 範例輸入

125

Sample Output / 範例輸出

2 minutes 5 seconds

C++ focus / 語法重點:Use / 60 and % 60.

ClassWork 1-8 · Total Cost

ItemDetails
Problem Statement / 題目說明Given price P and quantity Q, print the total cost. Then print the cost after adding a delivery fee D.
中文:計算貨品總價及加運費後總價。
Input Format / 輸入格式Three integers P, Q and D.
中文:單價、數量、運費。
Output Format / 輸出格式Line 1: item total. Line 2: final total.
中文:第一行貨品總價,第二行最終總價。
Constraints / 限制0 ≤ P, Q, D ≤ 10^6

Sample Input / 範例輸入

12 5 8

Sample Output / 範例輸出

60
68

C++ focus / 語法重點:First calculate P * Q; then use += D.

ClassWork 1-9 · Average Speed

ItemDetails
Problem Statement / 題目說明Given distance D in kilometres and time T in hours, print average speed D/T with exactly 2 decimal places.
中文:計算平均速度,固定輸出兩位小數。
Input Format / 輸入格式Two integers D and T.
中文:距離及時間。
Output Format / 輸出格式Print one real number with 2 decimal places.
中文:輸出兩位小數。
Constraints / 限制0 ≤ D ≤ 10^6; 1 ≤ T ≤ 10^6

Sample Input / 範例輸入

100 3

Sample Output / 範例輸出

33.33

C++ focus / 語法重點:Use fixed, setprecision(2), and divide by double(T).

ClassWork 1-10 · Temperature Difference

ItemDetails
Problem Statement / 題目說明Given two Celsius temperatures A and B, print the positive difference between them.
中文:輸出兩個溫度的正差值。
Input Format / 輸入格式Two integers A and B.
中文:兩個整數。
Output Format / 輸出格式Print one non-negative integer.
中文:輸出非負整數。
Constraints / 限制-100 ≤ A, B ≤ 100

Sample Input / 範例輸入

18 25

Sample Output / 範例輸出

7

C++ focus / 語法重點:Use if...else and subtraction; do not assume the input order.

ClassWork 1-11 · Full Boxes

ItemDetails
Problem Statement / 題目說明Each box holds K pencils. Given N pencils and K, print the number of full boxes and the remaining pencils.
中文:計算完整盒數及剩餘支數。
Input Format / 輸入格式Two positive integers N and K.
中文:總支數及每盒支數。
Output Format / 輸出格式Print boxes remainder on one line.
中文:同一行輸出盒數及剩餘數。
Constraints / 限制1 ≤ N, K ≤ 10^9

Sample Input / 範例輸入

53 12

Sample Output / 範例輸出

4 5

C++ focus / 語法重點:Use integer division and remainder.

ClassWork 1-12 · Pass Check

ItemDetails
Problem Statement / 題目說明Given a score S, print Pass when S is at least 50; otherwise print Fail.
中文:判斷及格或不及格。
Input Format / 輸入格式One integer S.
中文:一個整數。
Output Format / 輸出格式Print Pass or Fail.
中文:輸出 Pass 或 Fail。
Constraints / 限制0 ≤ S ≤ 100

Sample Input / 範例輸入

49

Sample Output / 範例輸出

Fail

C++ focus / 語法重點:Use >=, not =. An if...else is suitable.

ClassWork 1-13 · Teenager Check

ItemDetails
Problem Statement / 題目說明Given age A, print Teen when 13 ≤ A ≤ 19; otherwise print Not Teen.
中文:判斷是否青少年。
Input Format / 輸入格式One integer A.
中文:一個整數年齡。
Output Format / 輸出格式Print Teen or Not Teen.
中文:輸出 Teen 或 Not Teen。
Constraints / 限制0 ≤ A ≤ 120

Sample Input / 範例輸入

15

Sample Output / 範例輸出

Teen

C++ focus / 語法重點:Join two comparisons with &&.

ClassWork 1-14 · Weekend Check

ItemDetails
Problem Statement / 題目說明Given a day number D where 1 is Monday and 7 is Sunday, print Weekend for 6 or 7; otherwise print Weekday.
中文:判斷平日或週末。
Input Format / 輸入格式One integer D.
中文:一個星期數字。
Output Format / 輸出格式Print Weekend or Weekday.
中文:輸出 Weekend 或 Weekday。
Constraints / 限制1 ≤ D ≤ 7

Sample Input / 範例輸入

6

Sample Output / 範例輸出

Weekend

C++ focus / 語法重點:Use || to combine the two weekend cases.

ClassWork 1-15 · Discount Price

ItemDetails
Problem Statement / 題目說明Given purchase P, print the final price. Apply a 10% discount only when P is at least 100; print exactly 2 decimal places.
中文:消費滿 100 才有九折,輸出兩位小數。
Input Format / 輸入格式One real number P.
中文:一個實數金額。
Output Format / 輸出格式Print the final price with 2 decimal places.
中文:固定輸出兩位小數。
Constraints / 限制0 ≤ P ≤ 10^6

Sample Input / 範例輸入

120

Sample Output / 範例輸出

108.00

C++ focus / 語法重點:Use if, *= 0.9, fixed, and setprecision(2).

ClassWork 1-16 · Three-Digit Number

ItemDetails
Problem Statement / 題目說明Given an integer N, print YES if it is a three-digit positive number (100 to 999); otherwise print NO.
中文:判斷是否三位正整數。
Input Format / 輸入格式One integer N.
中文:一個整數。
Output Format / 輸出格式Print YES or NO.
中文:輸出 YES 或 NO。
Constraints / 限制-10^9 ≤ N ≤ 10^9

Sample Input / 範例輸入

1000

Sample Output / 範例輸出

NO

C++ focus / 語法重點:Use both >= 100 and <= 999 with &&.

ClassWork 1-17 · Operator Answer Drill (20 Sub-questions)

ItemDetails
Task / 題目Calculate the result of each expression. Write the answer in the blank. Do not write a C++ program for this exercise.
中文:計算每個運算式的結果,填寫答案;本練習不需要寫 C++ 程式。
Answer format / 答案格式Write an integer, a decimal, true, or false as required.
中文:按題目填整數、小數、truefalse
Bitwise rule / 位元規則For Questions 17 and 20, use exactly 8 bits.
中文:第 17、20 題必須以 8 位元計算。
No.Expression / 運算式Your answer / 你的答案
013 + 5Ans: ______
0214 - 7Ans: ______
036 * 4Ans: ______
0417 / 5 (both values are int)Ans: ______
0517 % 5Ans: ______
06(5 + 3) % 3 + 10Ans: ______
075 == 6Ans: ______
085 != 6Ans: ______
098 >= 8 && 3 < 5Ans: ______
10`8 < 8
11!falseAns: ______
12int x = 5; x += 3; What is x? 中文:x 的值是甚麼?Ans: ______
13int y = 9; y--; What is y? 中文:y 的值是甚麼?Ans: ______
1412 & 10Ans: ______
15`1210`
1612 ^ 10Ans: ______
17~13 using 8 bits. 中文:以 8 位元計算。Ans: ______
1810 & 1Ans: ______
19`(12 & 10)3`
2014 & ~4 using 8 bits. 中文:以 8 位元計算。Ans: ______

Hint / 提示:For Questions 14–20, write the decimal values as binary first. For example, 12 = 00001100₂ and 10 = 00001010₂.

Next Step / 下一步

Before Unit 2, you should be able to choose int or double, explain / and %, and distinguish && from &, and || from |. Redo Example 1-5 to 1-14 and ClassWork 1-17 if needed. 中文:進入 Unit 2 前,應能選擇合適型別,並正確使用基本運算子。

Built with LogoFlowershow