Berpikir komputasional
International Baccalaureate · IB Diploma · Computer Science · HL Topik 5 4:49 Narasi bahasa Inggris · Subtitle bahasa Inggris + 中文 disematkan langsung
Bab-bab
Transkrip
Your school wants a canteen app. Students order lunch on a phone, pay from a card balance, and the kitchen sees the orders.
学校想要一个食堂应用:学生用手机订午餐,从卡里余额付款,厨房看到订单。
Nobody can write that in one go.
没人能一口气写完。
Computational thinking is the set of habits that turns a big, vague problem into small, exact steps a computer can follow.
计算思维就是一套习惯,把一个又大又模糊的问题变成计算机能执行的、又小又精确的步骤。
Decomposition splits the app into log in, choose meal, pay and kitchen.
分解把应用拆成登录、选餐、付款和厨房。
Pattern recognition spots that every screen shows a list and takes a choice.
模式识别发现每个界面都是显示一个列表、接受一个选择。
Abstraction keeps what matters: a meal is a name, a price and its allergens, not its photo.
抽象保留重要的部分:一份餐是名称、价格和过敏原,而不是它的照片。
Algorithmic design writes the exact steps, such as how to deduct a price.
算法设计写出精确步骤,比如怎样扣除价格。
Each sub-problem splits again.
每个子问题还能再拆。
Log in becomes check the student ID and check the balance.
登录变成核对学号和查余额。
Choose meal becomes show the menu and filter allergens.
选餐变成显示菜单和筛选过敏原。
Pay becomes deduct the balance and print a receipt.
付款变成扣余额和打印收据。
Kitchen becomes list orders and mark them ready.
厨房变成列出订单和标记完成。
Each piece can be tested on its own.
每一块都可以单独测试。
Pattern recognition finds the same structure in different places.
模式识别在不同地方找出相同的结构。
A lunch order, a sports-kit order and a trip payment all add prices, then deduct from a balance, so one solution serves all three.
午餐订单、运动服订单和旅行付款都是先加总价格再从余额扣除,所以一个方案三处通用。
Abstraction builds a model that leaves out what does not matter, like a fire-drill map with exits but no wall colours.
抽象建立一个省略无关细节的模型,比如消防演习图有出口没有墙的颜色。
But a canteen model with no allergens would be dangerous.
但没有过敏原的食堂模型就很危险。
An algorithm is a finite sequence of exact steps.
算法是有限的一串精确步骤。
Each step must be unambiguous, with one meaning: add some salt is not a step, but add two grams of salt is.
每一步都必须无歧义,只有一种意思:“加点盐”不是一个步骤,“加两克盐”才是。
It must be finite, stopping after a limited number of steps.
它必须有限,在有限步后停止。
It must be correct for every valid input, and defined by its inputs and outputs.
它必须对每个有效输入都正确,并由输入和输出来定义。
Every algorithm is built from three constructs.
所有算法都由三种结构组成。
Sequence runs steps one after another.
顺序结构一步接一步执行。
Selection uses a decision to choose the path, drawn as a diamond with yes and no exits.
选择结构用判断来选路径,画成有“是”和“否”出口的菱形。
Iteration repeats steps, drawn as an arrow looping back.
循环结构重复步骤,画成往回绕的箭头。
Start and end go in rounded boxes, and input and output in parallelograms.
开始和结束放在圆角框里,输入和输出放在平行四边形里。
This flowchart finds the largest of three marks.
这个流程图找出三个分数中最大的。
Start with max and count at zero.
开始时 max 和 count 都设为零。
While count is less than three, input a mark; if it is greater than max, max takes its value.
当 count 小于三时,输入一个分数;如果它大于 max,就让 max 等于它。
Add one to count and loop back.
count 加一,再回到判断。
When count reaches three, output max.
count 到三时,输出 max。
Trace it with the inputs sixty-two, eighty-five and seventy-one.
用输入六十二、八十五、七十一来追踪。
Sixty-two is greater than zero, so max becomes sixty-two.
六十二大于零,max 变成六十二。
Eighty-five is greater, so max becomes eighty-five.
八十五更大,max 变成八十五。
Seventy-one is not greater, so max stays eighty-five.
七十一不更大,max 仍是八十五。
Count reaches three, and the output is eighty-five.
count 到三,输出八十五。
Now trace minus five, minus eight and minus two.
再追踪负五、负八、负二。
None of them is greater than zero, so max stays zero, and the output is zero, which is not one of the marks.
它们都不大于零,所以 max 一直是零,输出零,而零根本不是其中一个分数。
Starting max at zero was the error.
把 max 初始化为零就是错误所在。
Starting it at the first mark is safer.
用第一个分数作为初值更安全。
The same algorithm can be written as pseudocode, structured English that is close to code but not tied to one language.
同一个算法可以写成伪代码,也就是接近代码、但不依赖某种语言的结构化英语。
Set max and count to zero.
把 max 和 count 设为零。
Loop while count is less than three: input a mark, and if it is greater than max, store it in max.
当 count 小于三时循环:输入一个分数,如果它大于 max,就存进 max。
Add one to count.
count 加一。
After the loop, output max.
循环结束后输出 max。
Choose tests from the specification.
测试数据要根据规格说明来选。
For a mark from zero to a hundred, normal data is typical, like forty-five.
分数范围是零到一百时,正常数据是典型值,比如四十五。
Boundary data sits at the edges and just outside: zero, a hundred, minus one and a hundred and one.
边界数据在两端以及刚好越界:零、一百、负一和一百零一。
Invalid data must be rejected, like a hundred and fifty or the word ten.
无效数据必须被拒绝,比如一百五十或单词“ten”。
When a test fails, decide where the fault lies.
测试失败时,要判断问题出在哪里。
The algorithm itself may be wrong. The implementation, the code, may not match the algorithm. Or the requirement may never have covered that case.
可能是算法本身错了;可能是实现,也就是代码,与算法不符;也可能是需求从来没有考虑到这种情况。
Two correct algorithms can differ greatly in speed.
两个都正确的算法,速度可能相差很大。
Finding a name in a register of a thousand by checking each in turn takes up to a thousand steps.
在一千人的名册里逐个查找一个名字,最多要一千步。
Halving a sorted register each time, binary search, takes about ten.
每次把排好序的名册对半分,也就是二分查找,大约只要十步。
Try these. Decompose a sports day. Explain abstraction with a metro map. Trace the flowchart for forty, forty and ninety. And choose normal, boundary and invalid ages for a ticket price.
试试这些:分解组织运动会这个问题;用地铁线路图解释抽象;用四十、四十、九十追踪流程图;为门票价格选出正常、边界和无效的年龄。
In the exam, apply each approach to the question's own scenario.
考试时,把每种方法用到题目自己的情境中。
Use the correct flowchart shapes and label every exit.
流程图形状要正确,每个出口都要标注。
In trace tables, add a row each time a variable changes.
追踪表中变量每变一次就加一行。
And test both edges of the range and just outside.
边界测试要覆盖范围两端以及刚好越界的值。