← Course Hub← 课程主页 ← All Units← 返回单元列表
H I G H  S C H O O L  C O M P U T E R  S C I E N C E
Solutions答案与解析

Computational Thinking and Algorithms计算思维与算法

Worked Solutions · AP CSP-Feeder · US / ON / BC / AB Styles解题答案 · AP CSP 衔接 · 美 / 安 / 卑 / 阿省风格



PART I  ·  SHORT RESPONSE第一部分  ·  短答题Solutions · 25 marks答案 · 共 25 分

Section A · Short Response SolutionsA 部分 · 短答题答案

S1 EASY 🇺🇸 US §1 Computational Thinking Pillars计算思维支柱 [3 marks][3 分]

Answer: (C) Abstraction答案:(C) 抽象 A1

Abstraction means stripping away irrelevant detail and keeping only what matters for the problem at hand. The student is choosing to store only temperature and precipitation because those are the only fields relevant to her forecasts. Wind speed, humidity, and UV index are real data, but they are not needed for this program's purpose, so they are abstracted away.抽象意味着去除无关细节,只保留解决当前问题所需的内容。该学生选择只存储温度和降水量,因为这些是对她的天气预报唯一相关的字段。风速、湿度和紫外线指数是真实数据,但对此程序的目的不需要,因此被抽象掉。

Distractors: Decomposition (A) breaks a problem into sub-problems, not data selection. Pattern recognition (B) identifies repeated structures. Algorithmic thinking (D) writes step sequences.干扰项:分解 (A) 将问题拆分为子问题,而非选择数据。模式识别 (B) 识别重复结构。算法思维 (D) 编写步骤序列。

Abstraction is the pillar most commonly confused with decomposition on exams. The key distinguisher: decomposition is about splitting a problem into parts; abstraction is about filtering data or detail to only what matters. A map is the canonical example: it abstracts a city by keeping roads and landmarks but dropping building interiors. Whenever a question says "ignore X because it is not needed," that is abstraction.抽象是考试中最常与分解混淆的支柱。关键区别:分解是将问题拆分为部分;抽象是过滤数据或细节,只保留重要的内容。地图是经典例子:它通过保留道路和地标但去除建筑内部来抽象城市。每当题目说"忽略 X 因为它不需要",那就是抽象。
S2 EASY 🇺🇸 US §2 Algorithm Properties算法属性 [3 marks][3 分]

Answer: (C) It violates the finiteness property because it never terminates答案:(C) 它违反了有限性属性,因为它永远不会终止 A1

A valid algorithm must satisfy three properties: finiteness (terminates after a finite number of steps), definiteness (every step is precisely defined), and effectiveness (every step can actually be executed). WHILE TRUE: OUTPUT "Hello" loops forever because the condition is always true and there is no break or exit mechanism. It produces output (so B is wrong) and uses valid pseudocode syntax (so A is wrong). Finiteness is the violated property.有效算法必须满足三个属性:有限性(经过有限步骤后终止)、确定性(每个步骤都精确定义)和有效性(每个步骤都可以实际执行)。WHILE TRUE: OUTPUT "Hello" 永远循环,因为条件始终为真且没有 break 或退出机制。它产生输出(所以 B 错误)并使用有效的伪代码语法(所以 A 错误)。有限性是被违反的属性。

The three algorithm properties (finiteness, definiteness, effectiveness) are a reliable exam target. An infinite loop is the classic finiteness violation. AB CSE1110 outcome 1.6 requires students to write algorithms in "acceptable format" -- but regardless of format, an infinite loop is never acceptable as an algorithm because it can never produce a final result. When writing a loop in pseudocode or code, always ask: what condition guarantees this terminates?算法的三个属性(有限性、确定性、有效性)是可靠的考试考点。无限循环是经典的有限性违规。AB CSE1110 结果 1.6 要求学生以"可接受的格式"编写算法——但无论格式如何,无限循环永远不能作为算法被接受,因为它永远无法产生最终结果。编写循环时,始终问自己:什么条件保证它终止?
S3 MEDIUM 🇨🇦 ON §3 Flowcharts流程图 [7 marks][7 分]
(a) [4]

The four standard flowchart shapes:四种标准流程图形状:

  • Oval / Rounded rectangle (Terminal) -- represents Start or End. Every flowchart has exactly one Start and at least one End.椭圆 / 圆角矩形(终端)-- 代表开始或结束。每个流程图有且仅有一个开始,至少一个结束。 B1
  • Rectangle (Process) -- represents an action, calculation, or assignment step.矩形(处理)-- 代表操作、计算或赋值步骤。 B1
  • Parallelogram (Input/Output) -- represents reading data in or writing data out.平行四边形(输入/输出)-- 代表读取数据或写出数据。 B1
  • Diamond (Decision) -- represents a yes/no question. Exactly two arrows exit: one for YES, one for NO.菱形(判断)-- 代表是/否问题。恰好两条出口箭头:一条为"是",一条为"否"。 B1
(b) [2]

Two decision diamonds.两个判断菱形。 B1 The first diamond asks "is the number greater than 0?" and the second asks "does the number equal 0?". Each condition is a separate yes/no branch requiring its own diamond.第一个菱形问"数字是否大于 0?",第二个问"数字是否等于 0?"。每个条件是独立的是/否分支,需要各自的菱形。 B1

(c) [1]

Output: "Zero"输出:"Zero" B1

When the input is 0: the first diamond ("number > 0?") evaluates to NO, so we proceed to the second diamond ("number == 0?") which evaluates to YES, sending us to the "Output Zero" parallelogram.当输入为 0 时:第一个菱形("数字 > 0?")评估为否,因此进入第二个菱形("数字 == 0?")评估为是,导向"输出 Zero"平行四边形。

A common exam error is drawing only one diamond for an if/else-if chain. The rule is simple: one diamond per decision point. A three-outcome classification (positive / zero / negative) always needs exactly two diamonds because the first question eliminates one outcome and the second question splits the remaining two. Count the number of branching points in your pseudocode -- that is the number of diamonds you need.考试中常见错误是对 if/else-if 链只画一个菱形。规则很简单:每个决策点一个菱形。三结果分类(正/零/负)始终需要恰好两个菱形,因为第一个问题排除一个结果,第二个问题分割剩余两个。数一数你的伪代码中有多少个分支点——那就是你需要的菱形数量。
S4 MEDIUM 🇨🇦 AB §4 Sequencing and IPO顺序结构与 IPO [6 marks][6 分]
(a) [4]

Completed trace table (1 mark per correctly completed row, up to 4):完整追踪表(每行正确得 1 分,最多 4 分):

Lineabc
Start初始---
13--
235-
33515
410515

Working: Line 1: a = 3. Line 2: b = a + 2 = 3 + 2 = 5. Line 3: c = b * a = 5 * 3 = 15. Line 4: a = c - b = 15 - 5 = 10.过程:行 1:a = 3。行 2:b = a + 2 = 3 + 2 = 5。行 3:c = b * a = 5 * 3 = 15。行 4:a = c - b = 15 - 5 = 10。

(b) [1]

Output: 10输出:10 B1

(c) [1]

Input: none (values are hardcoded). Process: the four assignment statements that compute b, c, then the final value of a. Output: the value of a (10).输入:无(值已硬编码)。处理:计算 b、c 以及 a 最终值的四条赋值语句。输出:a 的值(10)。 B1

Notice that variable a is used on both the right and left sides of the assignment in line 4 (SET a TO c - b). The right side (c - b = 10) is evaluated first using the current values of c and b, then the result is written back into a. This overwrites the previous value of a (3). Students who try to read left-to-right as simultaneous will get the wrong answer. Always evaluate the right side completely before writing to the left side.注意变量 a 在第 4 行赋值的右侧和左侧都出现(SET a TO c - b)。右侧(c - b = 10)首先使用 c 和 b 的当前值求值,然后结果写回 a。这会覆盖 a 的前一个值(3)。试图从左到右同时读取的学生会得到错误答案。始终在写左侧之前完整求值右侧。
S5 MEDIUM 🇨🇦 BC §7 Tracing and Debugging追踪与调试 [6 marks][6 分]
(a) [3]

Trace table (1 mark per 2 correct rows, up to 3):追踪表(每 2 行正确得 1 分,最多 3 分):

Iter迭代i (before body)(循环体前)i < 4?i < 4?total (after body)(循环体后)i (after body)(循环体后)
11TRUE12
22TRUE33
33TRUE64
Check检查4FALSEexits退出4
(b) [1]

Actual output: 6实际输出:6 B1 (only 1 + 2 + 3 = 6 were summed; 4 was never added)(只计算了 1 + 2 + 3 = 6;4 从未被加入)

(c) [2]

Error type: Logic error错误类型:逻辑错误 B1 -- the code runs without crashing but produces the wrong answer (6 instead of 10).-- 代码运行不崩溃但产生错误答案(6 而非 10)。

Fix: change WHILE i < 4 to WHILE i <= 4修复:将 WHILE i < 4 改为 WHILE i <= 4 B1 (or equivalently WHILE i < 5). This is a classic off-by-one error.(或等价地 WHILE i < 5)。这是经典的"差一"错误。

The off-by-one error (using < instead of <=, or stopping one iteration early) is the single most common logic error in loop-based algorithms. The diagnostic method: always check the boundary value manually. Ask "what happens when i = 4?" With i < 4, the condition is FALSE so 4 is never added. With i <= 4, the condition is TRUE so 4 is added then i becomes 5 and the loop correctly exits. AB CSE1110 outcome 1.7 requires testing "for failure as well as success" -- boundary values like 4 (the last intended value) are exactly what must be tested."差一"错误(使用 < 而非 <=,或提前一次迭代停止)是基于循环的算法中最常见的逻辑错误。诊断方法:始终手动检查边界值。问"当 i = 4 时会发生什么?"使用 i < 4 时,条件为 FALSE,所以 4 永远不会被加。使用 i <= 4 时,条件为 TRUE,所以 4 被加入,然后 i 变为 5,循环正确退出。AB CSE1110 结果 1.7 要求"测试失败情况和成功情况"——像 4(最后一个预期值)这样的边界值正是必须测试的。
PART II  ·  EXTENDED RESPONSE第二部分  ·  简答题Solutions · 30 marks答案 · 共 30 分

Section B · Extended Response SolutionsB 部分 · 简答题答案

S6 EASY 🇺🇸 US 🇨🇦 ON §1 + §6 Decomposition applied分解应用 [5 marks][5 分]
(a) [3]

Accept any three of the following sub-problems (1 mark each, max 3):接受以下任意三个子问题(每个 1 分,最多 3 分):

  • Read five scores from the user (Input phase).从用户读取五个成绩(输入阶段)。 B1
  • Compute the sum of the five scores (Process phase).计算五个成绩的总和(处理阶段)。 B1
  • Compute the average (divide sum by 5) (Process phase).计算平均分(总和除以 5)(处理阶段)。 B1
  • Determine the letter grade by comparing average to thresholds (Process phase).通过比较平均分与阈值确定字母等级(处理阶段)。 B1
  • Output the grade (Output phase).输出等级(输出阶段)。 B1
(b) [2]

Data the program needs (accept any two):程序需要的数据(接受任意两个):

  • The five exam scores (numeric values).五个考试成绩(数值)。 B1
  • The number of scores (5, a constant used in the average formula).成绩数量(5,用于平均公式的常数)。 B1

Data the program does NOT need (accept any two):程序不需要的数据(接受任意两个):

  • Student name (irrelevant to computing an average).学生姓名(与计算平均分无关)。 B1
  • Student ID number.学生 ID 编号。
  • Date of the exams.考试日期。
  • Teacher name or class section.教师姓名或班级。
Notice that parts (a) and (b) test two different pillars: (a) tests decomposition (breaking the problem into sub-problems) and (b) tests abstraction (identifying which data is relevant). A well-decomposed program has each sub-problem corresponding to a small, testable function or block of code. If you find one sub-problem is too complex, decompose it further -- the level to stop at is when each piece is small enough to write in 5-10 lines of pseudocode.注意 (a) 和 (b) 测试两个不同的支柱:(a) 测试分解(将问题拆分为子问题),(b) 测试抽象(识别哪些数据是相关的)。分解良好的程序中,每个子问题对应一个小的、可测试的函数或代码块。如果发现某个子问题太复杂,进一步分解——停止分解的级别是每个部分小到可以用 5-10 行伪代码写出。
S7 MEDIUM 🇨🇦 ON 🇨🇦 BC §2 Pseudocode authoring伪代码编写 [8 marks][8 分]
(a) [2]

Input: a positive integer n.输入:正整数 n。 B1

Process: iterate through integers from 1 to n; for each odd integer, add it to a running total.处理:遍历从 1 到 n 的整数;对每个奇数,将其加到累计总和中。 B1

Output: the sum of all odd integers from 1 to n inclusive.输出:1 到 n(含)所有奇数之和。

(b) [4]

Model answer (other correct pseudocode accepted). Award: 1 mark INPUT/OUTPUT present; 1 mark accumulator initialised to 0; 1 mark correct loop range; 1 mark correct odd check or step-by-2 logic.参考答案(接受其他正确伪代码)。评分:1 分 INPUT/OUTPUT 存在;1 分累加器初始化为 0;1 分正确循环范围;1 分正确奇数检查或步长为 2 逻辑。

Version A -- using modulo to check odd:版本 A -- 用取模检查奇数:

INPUT n
SET sum TO 0
SET i TO 1
WHILE i <= n:
    IF i % 2 != 0 THEN
        SET sum TO sum + i
    END IF
    SET i TO i + 1
END WHILE
OUTPUT sum

Version B -- stepping by 2 (more efficient):版本 B -- 步长为 2(更高效):

INPUT n
SET sum TO 0
SET i TO 1
WHILE i <= n:
    SET sum TO sum + i
    SET i TO i + 2
END WHILE
OUTPUT sum
(c) [2]

Trace with n = 5 (using Version B for clarity). 1 mark for correct sequence of accumulator values; 1 mark for correct final answer.以 n = 5 追踪(使用版本 B)。1 分正确的累加器值序列;1 分正确最终答案。

ii <= 5?i <= 5?sum
Init初始-0
1TRUE1
3TRUE4
5TRUE9
7FALSEexits退出

Output: 9. (Odd numbers from 1 to 5: 1 + 3 + 5 = 9.)输出:9。(1 到 5 的奇数:1 + 3 + 5 = 9。)

Both versions are correct, but Version B is more efficient: it only visits odd values, making the loop run ceil(n/2) times instead of n times. This is a small example of the algorithmic thinking pillar at work -- recognising that stepping by 2 encodes the "odd" constraint directly into the loop structure, eliminating the need for a conditional check inside the loop. When writing pseudocode on exams, always verify your answer by tracing with a small concrete example (here n = 5) before moving on.两个版本都正确,但版本 B 更高效:它只访问奇数值,使循环运行 ceil(n/2) 次而非 n 次。这是算法思维支柱的一个小示例——认识到步长为 2 将"奇数"约束直接编码到循环结构中,消除了在循环内部进行条件检查的需要。在考试中编写伪代码时,在继续之前始终用一个小的具体例子(这里是 n = 5)追踪你的答案来验证。
S8 HARD 🇺🇸 US §5 Algorithm Efficiency算法效率 [9 marks][9 分]

List: [3, 9, 15, 22, 31, 44, 57, 71, 83, 96] (indices 0-9), target = 71.列表:[3, 9, 15, 22, 31, 44, 57, 71, 83, 96](索引 0-9),目标 = 71。

(a) [3]

Linear search trace (1 mark per 3 correct rows, up to 3):线性搜索追踪(每 3 行正确得 1 分,最多 3 分):

Step步骤Index索引Value== 71?== 71?
103No
219No
3215No
4322No
5431No
6544No
7657No
8771YES

Total steps: 8.总步数:8

(b) [3]

Binary search trace (1 mark per correct step, up to 3):二分搜索追踪(每步正确得 1 分,最多 3 分):

Step步骤lowhighmidlist[mid]list[mid]Action操作
10943171 > 31, search right向右
259771FOUND找到

Total steps: 2. (Step 1: mid = floor((0+9)/2) = 4, list[4] = 31. Step 2: low = 5, mid = floor((5+9)/2) = 7, list[7] = 71. Found.)总步数:2。(步骤 1:mid = floor((0+9)/2) = 4,list[4] = 31。步骤 2:low = 5,mid = floor((5+9)/2) = 7,list[7] = 71。找到。)

(c) [1]

Pre-condition: the list must be sorted (in ascending or descending order).前提条件:列表必须已排序(按升序或降序)。 B1 Without sorting, eliminating half the list based on a midpoint comparison is meaningless.没有排序,基于中点比较来排除一半列表是没有意义的。

(d) [2]

(i) Linear search worst case: 1,000 steps (must check every element). (ii) Binary search worst case: 10 steps (log2(1000) < 10; since 210 = 1024 > 1000, at most 10 halvings).(i)线性搜索最坏情况:1,000 步(必须检查每个元素)。(ii)二分搜索最坏情况:10 步(log2(1000) < 10;由于 210 = 1024 > 1000,最多 10 次减半)。 B1

Binary search is far more efficient because it eliminates half the remaining search space on each step, reducing 1,000 elements to 1 in only 10 steps instead of up to 1,000.二分搜索效率远高于线性搜索,因为它每步排除一半剩余搜索空间,只需 10 步就能将 1,000 个元素缩减到 1 个,而非最多 1,000 步。 B1

On this 10-item list, binary search found the target in 2 steps vs 8 for linear -- a 4x speedup. On 1,000 items it is 100x faster (10 vs 1,000 steps). On 1,000,000 items it is 50,000x faster (20 vs 1,000,000 steps). The efficiency gap grows dramatically with list size. This is why AP CSP's Big Idea 3.17 (AAP-4.A) calls out "reasonable time" vs "unreasonable time" algorithms -- on very large inputs only logarithmic algorithms remain practical.在这个 10 项列表中,二分搜索用 2 步找到目标,而线性搜索用 8 步——快 4 倍。在 1,000 项中快 100 倍(10 对比 1,000 步)。在 1,000,000 项中快 50,000 倍(20 对比 1,000,000 步)。效率差距随列表大小急剧增长。这就是为什么 AP CSP 大概念 3.17(AAP-4.A)区分"合理时间"与"不合理时间"算法——对于非常大的输入,只有对数算法仍然实用。
S9 HARD Honors荣誉级 🇺🇸 US 🇨🇦 ON §5 Big-O Efficiency Reasoning大 O 效率推理 [8 marks][8 分]
(a) [2]

Linear search: O(n) -- in the worst case it checks all n items, so the step count grows linearly with n.线性搜索:O(n) -- 在最坏情况下检查所有 n 项,因此步骤数随 n 线性增长。 B1

Binary search: O(log n) -- each step halves the search space, so the step count grows logarithmically with n.二分搜索:O(log n) -- 每步将搜索空间减半,因此步骤数随 n 对数增长。 B1

(b) [2]

n = 1,048,576 = 220. Binary search worst case = log2(n) = log2(220) = 20 steps.n = 1,048,576 = 220。二分搜索最坏情况 = log2(n) = log2(220) = 20 步 B1

Working: since 220 = 1,048,576, each halving reduces the space by a factor of 2. After 20 halvings: 220 / 220 = 1 item remains, so at most 20 comparisons are needed.过程:由于 220 = 1,048,576,每次减半将空间缩小 2 倍。20 次减半后:220 / 220 = 1 项剩余,因此最多需要 20 次比较。 B1

(c) [2]

Linear search worst case on 1,048,576 items: 1,048,576 steps. Binary search worst case: 20 steps.1,048,576 项的线性搜索最坏情况:1,048,576 步。二分搜索最坏情况:20 步。

Ratio: 1,048,576 / 20 = 52,429 times more steps for linear search.比率:1,048,576 / 20 = 52,429 倍线性搜索所需步骤更多。 B1

Practically: if binary search takes 1 millisecond, linear search would take over 52 seconds on the same list -- an unusable user experience for a search feature.实际意义:如果二分搜索需要 1 毫秒,线性搜索在同一列表上将需要超过 52 秒——对搜索功能来说是不可接受的用户体验。 B1

(d) [2]

A programmer would choose linear search when the list is not sorted, because binary search requires a pre-sorted list and sorting itself takes time.当列表未排序时,程序员会选择线性搜索,因为二分搜索需要预排序的列表,而排序本身也需要时间。 B1

Example scenario: searching an unsorted email inbox for a message containing a specific word -- the emails arrive in random order and sorting them before each search would not be worth the cost for a small inbox.场景示例:在未排序的电子邮件收件箱中搜索包含特定词的邮件——邮件以随机顺序到达,对于小收件箱而言,每次搜索前排序不值得付出这个代价。 B1

Big-O is about growth rate, not absolute speed. O(log n) does not mean "fast" in all cases; it means that doubling n only adds one more step. O(n) means doubling n doubles the steps. The ratio 52,429x shows why this matters at scale. The practical takeaway for exams: always state the pre-condition (sorted list) alongside the Big-O classification of binary search, because without it, binary search gives wrong answers, not just slower answers.大 O 是关于增长率,而不是绝对速度。O(log n) 并不意味着在所有情况下都"快";它意味着 n 加倍只增加一步。O(n) 意味着 n 加倍使步骤数加倍。52,429 倍的比率显示了这在规模上的重要性。考试的实际要点:始终在二分搜索的大 O 分类旁边说明前提条件(已排序列表),因为没有它,二分搜索给出错误答案,而不只是更慢的答案。
PART III  ·  MODELING / APPLIED第三部分  ·  建模与应用Solutions · 25 marks答案 · 共 25 分

Section C · Modeling and Applications SolutionsC 部分 · 建模与应用答案

S10 MEDIUM 🇺🇸 US 🇨🇦 ON §2 + §4 + §7 Greedy algorithm trace贪心算法追踪 [8 marks][8 分]
(a) [4]

Trace with amount = 41 (1 mark per correct denomination group, up to 4):以 amount = 41 追踪(每个面值组正确得 1 分,最多 4 分):

coin面值amount before扣除前金额amount after扣除后金额count
2541161
101662
5613
1104

Note: the FOR EACH coin loop advances once per denomination. The WHILE loop inside runs until the denomination no longer fits. For coins 25 and 10 and 5 the WHILE body each executes once; for coin 1 it executes once (amount 1 to 0). Total WHILE executions = 4.注意:FOR EACH coin 循环每个面值推进一次。内部的 WHILE 循环运行直到该面值不再合适。对于面值 25、10 和 5,WHILE 循环体各执行一次;对于面值 1,执行一次(金额从 1 到 0)。WHILE 总执行次数 = 4。

(b) [2]

Final count: 4最终 count:4 B1

Coins used: 25 + 10 + 5 + 1 = 41 cents. One coin of each denomination.使用的硬币:25 + 10 + 5 + 1 = 41 分。每种面值各一枚。 B1

(c) [2]

Pillar: Algorithmic thinking支柱:算法思维 B1 -- the greedy strategy is a precise, unambiguous, step-by-step procedure: "at each stage, take the largest coin that does not exceed the remaining amount." This step sequence is repeatable and produces a definite result, which is the definition of an algorithm.-- 贪心策略是一个精确、无歧义的逐步程序:"在每个阶段,取不超过剩余金额的最大面值硬币。"这个步骤序列可重复并产生确定结果,这正是算法的定义。 B1

The greedy coin algorithm works optimally for standard coin denominations (1, 5, 10, 25) but fails for unusual denominations. For example, with coins [1, 3, 4] and amount 6, greedy picks 4 + 1 + 1 = 3 coins, but the optimal solution is 3 + 3 = 2 coins. This is a preview of why algorithm choice matters -- the right algorithm depends on the structure of the problem, not just the goal.贪心找零算法对标准硬币面值(1、5、10、25)效果最优,但对非常规面值会失败。例如,对于硬币 [1, 3, 4] 和金额 6,贪心选择 4 + 1 + 1 = 3 枚硬币,但最优解是 3 + 3 = 2 枚硬币。这预示了为什么算法选择很重要——正确的算法取决于问题的结构,而不仅仅是目标。
S11 MEDIUM 🇨🇦 ON 🇨🇦 BC §3 + §6 + §7 Flowchart, decomposition, debugging流程图、分解、调试 [8 marks][8 分]
(a) [4]

Model pseudocode (1 mark per structural element: INPUT line; outer IF/ELSE; inner IF/ELSE; correct END IF nesting):参考伪代码(每个结构元素 1 分:INPUT 行;外层 IF/ELSE;内层 IF/ELSE;正确的 END IF 嵌套):

INPUT password
IF LENGTH(password) >= 8 THEN
    IF CONTAINS_DIGIT(password) THEN
        OUTPUT "Strong"
    ELSE
        OUTPUT "Add a digit"
    END IF
ELSE
    OUTPUT "Too short"
END IF

Accept equivalent pseudocode. The key requirements: INPUT first, outer condition on length, inner condition on digit presence, correct branch outputs.接受等价伪代码。关键要求:INPUT 在第一行,外层条件检查长度,内层条件检查是否含数字,正确的分支输出。

(b) [2]

Updated sub-problems (accept any three, 1 mark per sub-problem, max 2 since only 2 marks available):更新后的子问题(接受任意三个,每个子问题 1 分,由于只有 2 分,最多 2 分):

  • Check that the password has at least 8 characters (minimum length).检查密码至少有 8 个字符(最小长度)。 B1
  • Check that the password does not exceed 20 characters (maximum length).检查密码不超过 20 个字符(最大长度)。 B1
  • Check that the password contains at least one digit.检查密码包含至少一个数字。 B1
  • Output an appropriate message for each outcome (Strong, Too short, Too long, Add a digit).为每种结果输出适当的消息(Strong、Too short、Too long、Add a digit)。
(c) [2]

Output: "Too short"输出:"Too short" B1

Path: START → Read password ("hello") → Diamond "length ≥ 8?" (5 ≥ 8 is FALSE) → NO branch → Output "Too short" → END.路径:START → 读取密码("hello")→ 菱形"长度 ≥ 8?"(5 ≥ 8 为否)→ 否分支 → 输出"Too short" → END。 B1 The digit check is never reached because the length check fails first.数字检查从未到达,因为长度检查首先失败。

The nested structure here is critical: the digit check is only reached if the length check passes. This is deliberate -- it avoids giving misleading feedback ("Add a digit") to a password that is also too short. On exams, when converting a flowchart to pseudocode, first identify the flowchart's diamond hierarchy (which diamond is checked first, which is nested inside a YES branch), then write the pseudocode with matching IF nesting. The nesting order in the pseudocode must exactly mirror the conditional sequence in the flowchart.这里的嵌套结构至关重要:只有在长度检查通过后才会到达数字检查。这是刻意设计的——它避免给同样太短的密码提供误导性反馈("Add a digit")。在考试中,将流程图转换为伪代码时,首先识别流程图的菱形层次结构(哪个菱形首先被检查,哪个嵌套在是分支内),然后用匹配的 IF 嵌套编写伪代码。伪代码中的嵌套顺序必须与流程图中的条件序列完全镜像。
S12 HARD 🇺🇸 US 🇨🇦 ON 🇨🇦 BC All sections · Full design + trace全节综合 · 完整设计与追踪 [9 marks][9 分]
(a) [2]

Three high-level sub-problems (1 mark per correct sub-problem, max 2 since only 2 marks available):三个高层子问题(每个正确子问题 1 分,由于只有 2 分,最多 2 分):

  • Accept the student's requested book title as input.接受学生请求的书名作为输入。 B1
  • Search the sorted catalogue for the requested title.在已排序目录中搜索请求的书名。 B1
  • Return or display whether the title was found (available / not available).返回或显示书名是否找到(可用/不可用)。 B1
(b) [2]

Binary search is more appropriate.二分搜索更合适。 B1

Binary search on a 5,000-title sorted catalogue needs at most log2(5000) < 13 steps to find any title. Linear search needs up to 5,000 steps in the worst case. Since the catalogue is already sorted (a required pre-condition that holds here), binary search is 5000/13 ≈ 385 times more efficient.在 5,000 个书名的已排序目录上,二分搜索最多需要 log2(5000) < 13 步找到任何书名。线性搜索最坏情况下需要最多 5,000 步。由于目录已排序(此处满足的必要前提条件),二分搜索效率约高 5000/13 ≈ 385 倍。 B1

(c) [4]

Model pseudocode procedure (1 mark per structural element: correct variable initialisation; correct WHILE condition; correct mid computation; correct return logic):参考伪代码过程(每个结构元素 1 分:正确的变量初始化;正确的 WHILE 条件;正确的 mid 计算;正确的返回逻辑):

PROCEDURE SEARCH(catalogue, target):
    SET low TO 0
    SET high TO LENGTH(catalogue) - 1
    WHILE low <= high:
        SET mid TO FLOOR((low + high) / 2)
        IF catalogue[mid] == target THEN
            RETURN TRUE
        ELSE IF catalogue[mid] < target THEN
            SET low TO mid + 1
        ELSE
            SET high TO mid - 1
        END IF
    END WHILE
    RETURN FALSE
END PROCEDURE

Award marks: [B1] low=0, high=LENGTH-1 initialised. [B1] WHILE low <= high (loop terminates correctly). [B1] mid = FLOOR((low+high)/2). [B1] Returns TRUE when found, adjusts low or high correctly, returns FALSE after loop.评分:[B1] low=0,high=LENGTH-1 初始化。[B1] WHILE low <= high(循环正确终止)。[B1] mid = FLOOR((low+high)/2)。[B1] 找到时返回 TRUE,正确调整 low 或 high,循环后返回 FALSE。

(d) [1]

Logic error.逻辑错误。 B1 The procedure would not crash (no syntax error, no runtime crash for most inputs), but it would produce wrong answers: when the mid element is compared to the target on an unsorted list, the decision to search left or right is based on a false assumption that smaller values are to the left and larger values to the right. The procedure may report FALSE (not found) for a title that actually exists, or TRUE for a wrong position.该过程不会崩溃(无语法错误,对大多数输入无运行时崩溃),但会产生错误答案:当在未排序列表上将中间元素与目标比较时,向左或向右搜索的决策基于错误假设——较小值在左边,较大值在右边。该过程可能为实际存在的书名报告 FALSE(未找到),或在错误位置报告 TRUE。

Q12 integrates all four computational thinking pillars in one scenario: Decomposition (sub-problems in part a), Algorithmic thinking (writing the procedure in part c), Abstraction (the procedure hides low/mid/high bookkeeping behind a clean SEARCH interface), and implicitly Pattern recognition (recognising that halving the space is the same pattern as all binary search problems). This integration is what AP CSP and ICS3U assess at the highest level. When you write any search/sort procedure on an exam, always verify it by tracing with a tiny list (3-4 items) before moving on.Q12 在一个场景中整合了全部四个计算思维支柱:分解((a) 部分的子问题)、算法思维((c) 部分编写过程)、抽象(该过程将 low/mid/high 记账操作隐藏在简洁的 SEARCH 接口后面),以及隐含的模式识别(认识到将空间减半是所有二分搜索问题的相同模式)。这种整合是 AP CSP 和 ICS3U 在最高水平评估的内容。在考试中编写任何搜索/排序过程时,始终用一个小列表(3-4 项)追踪来验证,然后再继续。