Master the fundamentals of algorithmic thinking and problem-solving
掌握算法思維和問題解決的基礎知識
Learn what algorithms are, how to represent them, and practice dry running
學習什麼是算法,如何表示它們,以及練習演練
Understand variables, naming rules, and assignment operations
理解變量、命名規則和賦值操作
Explore different data types and how to choose the right one
探索不同的數據類型以及如何選擇合適的類型
Master sequence, selection, and iteration control structures
掌握序列、選擇和迭代控制結構
Learn about conditional statements and decision-making
學習條件語句和決策制定
Test your knowledge with 15 comprehensive questions
通過15個綜合問題測試您的知識
An algorithm is a set of steps for solving a problem in the specified order. We can deduce from an algorithm its use and output step-by-step. This process is called dry running or tracing.
Algorithms can be represented in two main ways:
Pseudocode uses plain language and simple notation to describe the logic of an algorithm without worrying about specific programming syntax.
A flowchart uses standardized symbols to represent the flow of an algorithm visually.
| Symbol | Name | Purpose |
|---|---|---|
| Oval | Terminal | Start or End of algorithm |
| Parallelogram | Input/Output | Read data or display results |
| Rectangle | Process | Perform calculations or operations |
| Diamond | Decision | Make a choice based on a condition |
| Arrow | Flow line | Show direction of flow |
算法是一組按指定順序解決問題的步驟。我們可以從算法中逐步推斷其用途和輸出。這個過程稱為演練或追蹤。
算法可以用兩種主要方式表示:
偽代碼使用簡單的語言和符號來描述算法的邏輯,而不必擔心特定的編程語法。
流程圖使用標準化符號來可視化表示算法的流程。
| 符號 | 名稱 | 用途 |
|---|---|---|
| 橢圓形 | 終端 | 算法的開始或結束 |
| 平行四邊形 | 輸入/輸出 | 讀取數據或顯示結果 |
| 矩形 | 處理 | 執行計算或操作 |
| 菱形 | 決策 | 根據條件做出選擇 |
| 箭頭 | 流線 | 顯示流程方向 |
A variable is a named storage location in computer memory that can hold a value. Think of it as a labeled box where you can store and retrieve data.
The assignment operator (←) assigns a value to a variable:
| Operator | Operation | Example | Result |
|---|---|---|---|
| + | Addition | 5 + 3 | 8 |
| - | Subtraction | 5 - 3 | 2 |
| × | Multiplication | 5 × 3 | 15 |
| / | Division | 6 / 3 | 2 |
| MOD | Modulus (remainder) | 7 MOD 3 | 1 |
| DIV | Integer division | 7 DIV 3 | 2 |
To swap the values of two variables, we need a temporary variable:
變量是計算機內存中的命名存儲位置,可以保存值。把它想像成一個標有標籤的盒子,您可以在其中存儲和檢索數據。
賦值運算符(←)將值分配給變量:
| 運算符 | 操作 | 示例 | 結果 |
|---|---|---|---|
| + | 加法 | 5 + 3 | 8 |
| - | 減法 | 5 - 3 | 2 |
| × | 乘法 | 5 × 3 | 15 |
| / | 除法 | 6 / 3 | 2 |
| MOD | 取模(餘數) | 7 MOD 3 | 1 |
| DIV | 整數除法 | 7 DIV 3 | 2 |
要交換兩個變量的值,我們需要一個臨時變量:
A data type defines the kind of data a variable can store and the operations that can be performed on it.
數據類型定義了變量可以存儲的數據類型以及可以對其執行的操作。
Examples: -5, 0, 42, 1000
Use for: Counting, indexing, quantities
Range: Typically -2,147,483,648 to 2,147,483,647
示例:-5, 0, 42, 1000
用於:計數、索引、數量
範圍:通常為-2,147,483,648至2,147,483,647
Examples: 3.14, -0.5, 2.718
Use for: Measurements, scientific calculations, currency
Precision: Approximately 6-7 decimal places
示例:3.14, -0.5, 2.718
用於:測量、科學計算、貨幣
精度:大約6-7位小數
Examples: 'A', 'z', '5', '@'
Use for: Single letters, symbols, digits
Storage: 1 byte (ASCII) or 2 bytes (Unicode)
示例:'A', 'z', '5', '@'
用於:單個字母、符號、數字
存儲:1字節(ASCII)或2字節(Unicode)
Examples: "Hello", "John Smith", "12345"
Use for: Names, addresses, text messages
Note: Sequence of characters
示例:"Hello", "John Smith", "12345"
用於:姓名、地址、文本消息
注意:字符序列
Values: True, False
Use for: Flags, conditions, logical operations
Storage: 1 bit (but usually 1 byte)
值:True, False
用於:標誌、條件、邏輯操作
存儲:1位(但通常為1字節)
Selecting the appropriate data type is crucial for:
Example: Student Management System
| Variable | Data Type | Reason | Example Value |
|---|---|---|---|
| studentID | Integer | Unique whole number identifier | 20231001 |
| studentName | String | Text data for full name | "Chan Tai Man" |
| grade | Character | Single letter grade | 'A' |
| gpa | Real | Decimal precision needed | 3.75 |
| isPassed | Boolean | True/False status | True |
選擇適當的數據類型對於以下方面至關重要:
示例:學生管理系統
| 變量 | 數據類型 | 原因 | 示例值 |
|---|---|---|---|
| 學生ID | 整數 | 唯一的整數標識符 | 20231001 |
| 學生姓名 | 字符串 | 全名的文本數據 | "陳大文" |
| 等級 | 字符 | 單個字母等級 | 'A' |
| 平均績點 | 實數 | 需要小數精度 | 3.75 |
| 是否通過 | 布爾值 | 真/假狀態 | True |
Computer algorithms use three fundamental control structures:
Instructions are executed one after another in order.
Choose different paths based on a condition.
Repeat a set of instructions multiple times.
Most algorithms follow the Input-Process-Output pattern:
計算機算法使用三種基本控制結構:
指令按順序一個接一個地執行。
根據條件選擇不同的路徑。
多次重複一組指令。
大多數算法遵循輸入-處理-輸出模式:
Selection uses Boolean expressions that evaluate to True or False.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| = | Equal to | 5 = 5 | True |
| ≠ | Not equal to | 5 ≠ 3 | True |
| < | Less than | 3 < 5 | True |
| > | Greater than | 5 > 3 | True |
| ≤ | Less than or equal | 5 ≤ 5 | True |
| ≥ | Greater than or equal | 5 ≥ 3 | True |
| Operator | Description | Example |
|---|---|---|
| AND | Both conditions must be true | (age ≥ 18) AND (hasLicense = True) |
| OR | At least one condition must be true | (day = "Saturday") OR (day = "Sunday") |
| NOT | Reverses the condition | NOT (isRaining = True) |
Proper indentation makes code structure clear and prevents logic errors. Each nested level should be indented consistently (usually 4 spaces).
選擇使用布爾表達式,其結果為True或False。
| 運算符 | 含義 | 示例 | 結果 |
|---|---|---|---|
| = | 等於 | 5 = 5 | True |
| ≠ | 不等於 | 5 ≠ 3 | True |
| < | 小於 | 3 < 5 | True |
| > | 大於 | 5 > 3 | True |
| ≤ | 小於或等於 | 5 ≤ 5 | True |
| ≥ | 大於或等於 | 5 ≥ 3 | True |
| 運算符 | 描述 | 示例 |
|---|---|---|
| AND | 兩個條件都必須為真 | (年齡 ≥ 18) AND (有駕照 = True) |
| OR | 至少一個條件必須為真 | (日期 = "星期六") OR (日期 = "星期日") |
| NOT | 反轉條件 | NOT (下雨 = True) |
正確的縮進使代碼結構清晰,並防止邏輯錯誤。每個嵌套級別都應該一致地縮進(通常為4個空格)。
Test your understanding of Algorithm Design concepts!
測試您對算法設計概念的理解!
Click on any card to flip it and reveal the definition!
點擊任何卡片以翻轉並顯示定義!