Unit 1 · Computer Basics and C++ Foundation
Unit 1 · Computer Basics and C++ Foundation
Start Here / 開始做題
- Read Input and Output first. 中文:先找輸入及輸出。
- Write down the variables you need. 中文:先決定變數。
- Trace the sample once by hand. 中文:用範例手算一次。
- Write, run, then compare every output character. 中文:執行後逐字比較輸出。
Unit Dashboard / 單元地圖
| Learn | Use in C++ | Try |
|---|---|---|
| How a computer handles data | input → process → output → storage | Example 1-1 to 1-4 |
| How data is measured | bit, byte, KB, MB, GB | ClassWork 1-1 |
| How a C++ program works | main, cin, cout, variables | ClassWork 1-2 to 1-4 |
| How to use data and operators | types, arithmetic, comparison, logic | Example 1-2 to 1-12; ClassWork 1-3 to 1-16 |
| How to make decisions and repeat | %, if...else, for | ClassWork 1-5, 1-12 to 1-16 |
Computer Basics / 計算機概論
| Idea | Short explanation |
|---|---|
| Input | Data enters the computer: keyboard, mouse, sensor. 中文:輸入資料。 |
| Process | The CPU follows instructions to transform data. 中文:按指令處理資料。 |
| Output | The result is shown, played or saved. 中文:輸出結果。 |
| Storage | Data is kept in memory or files. 中文:儲存資料。 |
Storage Units / 儲存單位
| Unit | Meaning |
|---|---|
| bit | One binary digit: 0 or 1. 中文:最小資料單位。 |
| byte | 8 bits. Often enough for one ASCII character. 中文:8 個 bit。 |
| KB / MB / GB | 1024 bytes / 1024 KB / 1024 MB in this course. |
| Character encoding | English letters often use 1 byte; Chinese UTF-8 characters commonly use 3 bytes. 中文:實際大小取決於編碼。 |
C++ Starter Kit / C++ 入門
| Part | Purpose |
|---|---|
#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 / 黃色語法重點
| Syntax | Meaning |
|---|---|
#include <iostream> | allows cin and cout |
int main() | program starts here |
cin >> variable | read input |
cout << value | print output |
return 0; | program ends normally |
Data Types and Operators / 資料型別及運算子
| Item | Use | Example |
|---|---|---|
int | whole number / 整數 | int age = 15; |
long long | large whole number / 大整數 | long long bytes; |
double | decimal number / 小數 | double average; |
string | one word or text / 文字 | string name; |
+ - * / % | arithmetic operators / 運算子 | 17 % 2 is 1 |
== != < > <= >= | comparison / 比較 | score >= 50 |
| `&& | !` | |
| `& | ^ ~` | bitwise operators / 位元運算子 |
Operator Guide / 運算子完整導覽
| Group | Operators | Meaning / 中文 | F3 note |
|---|---|---|---|
| Assignment | = | store a value / 儲存數值 | = is not “equal to”. |
| Arithmetic | + - * / % | add, subtract, multiply, divide, remainder / 加減乘除、餘數 | % works with integers. |
| Integer division | a / b | quotient without decimal when both are int / 整數除法 | 7 / 2 is 3; use 7.0 / 2 for 3.5. |
| Unary sign | +x, -x | positive 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 | ==`&& | !`== | |
| Conditional | condition ? A : B | choose 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. 中文:位元運算子直接處理二進位的每一位。
| Operator | Rule / 中文 | 4-bit example |
|---|---|---|
a & b | AND: both bits are 1 → 1 / 兩位皆為 1 才是 1 | 1100 & 1010 = 1000 |
| ==`a | b`== | OR: either bit is 1 → 1 / 有一位為 1 就是 1 |
a ^ b | XOR: different bits → 1 / 不同為 1,相同為 0 | 1100 ^ 1010 = 0110 |
~a | NOT: reverse every bit / 每一位反轉 | for 8 bits: ~00001101 = 11110010 |
Do Not Mix These / 不要混淆
| Symbol | Use | Example |
|---|---|---|
&& | logical AND: combine true/false conditions | age >= 12 && age <= 18 |
& | bitwise AND: compare binary bits | 12 & 10 gives 8 |
| ==` | `== | |
| ==` | `== | bitwise OR: combine binary bits |
Bitwise Dry Run / 位元手算
For a = 12 (1100₂) and b = 10 (1010₂):
| Operation | Binary result | Decimal result |
|---|---|---|
a & b | 1000₂ | 8 |
| `a | b` | 1110₂ |
a ^ b | 0110₂ | 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。
| Position | Binary mask | Decimal mask |
|---|---|---|
| bit 0 | 0001₂ | 1 |
| bit 1 | 0010₂ | 2 |
| bit 2 | 0100₂ | 4 |
| bit 3 | 1000₂ | 8 |
For ~, always state the width. This unit uses 8 bits in the NOT example so the output is predictable.
Operator Order / 運算次序
| Priority | Do first | Example |
|---|---|---|
| 1 | parentheses / 括號 | (a + b) * c |
| 2 | unary -, !, ++, -- | -x |
| 3 | *, /, % | a + b * c |
| 4 | +, - | a - b + c |
| 5 | comparisons | score >= 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!
| Item | Details |
|---|---|
| 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
| Item | Details |
|---|---|
| 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
| Item | Details |
|---|---|
| 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
| Item | Details |
|---|---|
| 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
| Item | Details |
|---|---|
| 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
| Item | Details |
|---|---|
| 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
| Item | Details |
|---|---|
| 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
| Item | Details |
|---|---|
| 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
| Item | Details |
|---|---|
| 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
| Item | Details |
|---|---|
| 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
| Item | Details |
|---|---|
| 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
| Item | Details |
|---|---|
| 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
| Item | Details |
|---|---|
| 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
| Item | Details |
|---|---|
| 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
| Item | Details |
|---|---|
| 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
| Item | Details |
|---|---|
| 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
| Item | Details |
|---|---|
| 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
| Item | Details |
|---|---|
| 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
| Item | Details |
|---|---|
| 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
| Item | Details |
|---|---|
| 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
| Item | Details |
|---|---|
| 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
| Item | Details |
|---|---|
| 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
| Item | Details |
|---|---|
| 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
| Item | Details |
|---|---|
| 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
| Item | Details |
|---|---|
| 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
| Item | Details |
|---|---|
| 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
| Item | Details |
|---|---|
| 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
| Item | Details |
|---|---|
| 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
| Item | Details |
|---|---|
| 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
| Item | Details |
|---|---|
| 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 Practice Set (20 Sub-questions)
| Item | Details |
|---|---|
| Problem Statement / 題目說明 | Complete 20 independent mini-programs about C++ operators. Each sub-question is a separate program. 中文:完成 20 個獨立的小程式,每小題獨立編寫。 |
| Input Format / 輸入格式 | Use the input shown for each sub-question. 中文:每小題按表格指定輸入。 |
| Output Format / 輸出格式 | Match the required output for that sub-question exactly. 中文:每小題必須完全符合指定輸出。 |
| Submission / 提交 | Submit 20 files named CW1-17-01.cpp to CW1-17-20.cpp.中文:提交 20 個對應檔案。 |
| No. | Mini-program / 小題 | Input | Required output / 範例輸出 | Syntax clue / 語法提示 |
|---|---|---|---|---|
| 01 | Print the sum of A and B. 中文:輸出和。 | 8 3 | 11 | + |
| 02 | Print A minus B. 中文:輸出差。 | 8 3 | 5 | - |
| 03 | Print A times B. 中文:輸出乘積。 | 8 3 | 24 | * |
| 04 | Print integer quotient then remainder. 中文:輸出整數商及餘數。 | 17 5 | 3 2 | /, % |
| 05 | Print the average of three marks with 2 decimal places. 中文:輸出三個分數平均值。 | 80 75 90 | 81.67 | double, / 3.0 |
| 06 | Start score S, add A, subtract B; print final score. 中文:加分扣分後輸出分數。 | 50 12 7 | 55 | +=, -= |
| 07 | Print N, then increase it by one and print it again. 中文:輸出 N,遞增後再輸出。 | 9 | 9 then 10 | ++ |
| 08 | Print the larger of A and B. 中文:輸出較大值。 | 8 13 | 13 | ?: or if |
| 09 | Print YES when N is from 10 to 99 inclusive. 中文:判斷是否介乎 10 至 99。 | 42 | YES | >=, <=, && |
| 10 | Print Weekend when D is 6 or 7; otherwise Weekday. 中文:判斷週末或平日。 | 6 | Weekend | ` |
| 11 | Print bitwise AND of A and B. 中文:輸出位元 AND。 | 12 10 | 8 | & |
| 12 | Print bitwise OR of A and B. 中文:輸出位元 OR。 | 12 10 | 14 | ` |
| 13 | Print bitwise XOR of A and B. 中文:輸出位元 XOR。 | 12 10 | 6 | ^ |
| 14 | Given N (0–255), print its 8-bit NOT value. 中文:輸出 8 位元 NOT。 | 13 | 11110010 | ~, bitset<8>, & 255 |
| 15 | Print Odd or Even using the last bit of N. 中文:用最低位判斷奇偶。 | 13 | Odd | n & 1 |
| 16 | Given N, print whether bit 2 is 1. Use mask 4 (0100₂). 中文:判斷 bit 2 是否為 1。 | 10 | 0 | n & 4 |
| 17 | Given N, set bit 2 to 1 and print the result. Use mask 4 (0100₂). 中文:把 bit 2 設為 1。 | 10 | 14 | `n |
| 18 | Given N, clear bit 2 to 0 and print the result. Use mask 4 (0100₂). 中文:把 bit 2 設為 0。 | 14 | 10 | n & ~4 |
| 19 | Given N, toggle bit 2 and print the result. Use mask 4 (0100₂). 中文:反轉 bit 2。 | 10 | 14 | n ^ 4 |
| 20 | Given A, B and C, print `(A & B) | C`. 中文:計算指定的位元運算式。 | 12 10 3 | 11 |
Check before submission / 提交前自查:Questions 11–20 use bitwise operators; do not replace & with && or | with ||.
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 前,應能選擇合適型別,並正確使用基本運算子。