跳到主要内容

算法设计与问题求解

IGCSE 计算机科学 · 第 7 主题

训练
讲义 词汇表
大纲
Candidates should be able to: Notes and guidance
1 Understand the program development life cycle, limited to: analysis, design, coding and testing • Including identifying each stage and performing these tasks for each stage: – analysis: abstraction, decomposition of the problem, identification of the problem and requirements – design: decomposition, structure diagrams, flowcharts, pseudocode – coding: writing program code and iterative testing – testing: testing program code with the use of test data
2 (a) Understand that every computer system is made up of sub-systems, which are made up of further sub-systems (b) Understand how a problem can be decomposed into its component parts • Including: – inputs – processes – outputs – storage
(c) Use different methods to design and construct a solution to a problem • Including: – structure diagrams – flowcharts – pseudocode
3 Explain the purpose of a given algorithm • Including: – stating the purpose of an algorithm – describing the processes involved in an algorithm
4 Understand standard methods of solution • Limited to: – linear search – bubble sort – totalling – counting – finding maximum, minimum and average values
5 (a) Understand the need for validation checks to be made on input data and the different types of validation check • Including: – range check – length check – type check – presence check – format check – check digit
(b) Understand the need for verification checks to be made on input data and the different types of verification check • Including: – visual check – double entry check
6 Suggest and apply suitable test data • Limited to: – normal – abnormal – extreme – boundary • Extreme data is the largest/smallest acceptable value • Boundary data is the largest/smallest acceptable value and the corresponding smallest/largest rejected value
7 Complete a trace table to document a dry-run of an algorithm • Including, at each step in an algorithm: – variables – outputs – user prompts
8 Identify errors in given algorithms and suggest ways of correcting these errors
9 Write and amend algorithms for given problems or scenarios, using: pseudocode, program code and flowcharts • Precision is required when writing algorithms, e.g. x > y is acceptable but x is greater than y is not acceptable • See section 4 for flowchart symbols • See section 4 for pseudocode

来源:剑桥国际大纲

7.1

程序开发生命周期

程序开发生命周期(program development life cycle)是用来制作一个程序的一套阶段。有四个阶段。

一个程序员在一台计算机前键入代码
软件由程序员编写,他们遵循开发生命周期
阶段 你做什么
分析(analysis) 研究问题并算出需要什么
设计(design) 规划程序将如何工作
编码(coding) 编写程序代码并随手测试它
测试(testing) 用测试数据运行完成的程序以找到错误
一排四个阶段——分析、设计、编码、测试——一个箭头从测试反馈到设计
程序开发的四个阶段;测试反馈以修复和精炼设计
一个带处理框和判断菱形的程序流程图
一个程序流程图在设计阶段布置一个程序的步骤和判断

Analysis

在分析中你理解问题。两个关键技能有帮助:

  • 抽象(abstraction)——只保留重要的细节并忽略其余的;
  • 分解(decomposition)——把一个大问题分成更小、更容易的部分。

Design

在设计中你规划解决方案,常常用分解。你可以把各部分显示为一个结构图(structure diagram,一个把一个系统分成更小的框的图)中的子系统(sub-systems)。

Coding and testing

编码中你编写程序代码。你用迭代测试(iterative testing)——在你构建小部分时一遍又一遍地测试它们。在测试中你用测试数据(test data)运行整个程序以检查它工作。

词汇表 训练
英文 中文 拼音
program development life cycle 程序开发生命周期 chéng xù kāi fā shēng mìng zhōu qī
analysis 分析 fēn xī
design 设计 shè jì
coding 编码 biān mǎ
testing 测试 cè shì
abstraction 抽象 chōu xiàng
decomposition 分解 fēn jiě
sub-systems 子系统 zi xì tǒng
structure diagram 结构图 jié gòu tú
iterative testing 迭代测试 dié dài cè shì
test data 测试数据 cè shì shù jù
flowchart 流程图 liú chéng tú
7.2

设计工具

你可以用三种主要方式规划一个解决方案。

  • 一个结构图(structure diagram)——显示一个系统的各部分以及它们如何组合在一起;
  • 一个流程图(flowchart)——一个用框和箭头按顺序显示步骤的图;
  • 伪代码(pseudocode)——用简单的、类似代码的英语书写的步骤(不是一门真实的语言)。
一个把数字 1 加到 n 的流程图,带开始/结束、输入/输出、处理和判断符号,加上一个命名每个形状的图例
求和算法的一个流程图,用标准符号(开始/结束、输入/输出、处理、判断)
词汇表 训练
英文 中文 拼音
pseudocode 伪代码 wěi dài mǎ
7.3

算法

冒泡排序,一趟接一趟

一个算法(algorithm)是解决一个问题的一套步骤,以正确的顺序。每个算法能被分成三部分:

  • 输入(input)——进去的数据;
  • 处理(processing)——对数据做的工作;
  • 输出(output)——出来的结果。

这叫分解成输入、处理和输出。例如,对于"求三个分数的平均值":输入是三个分数;处理是把它们相加并除以 3;输出是平均值。

三个框——INPUT(3 个分数)、PROCESS(把它们相加,除以 3)、OUTPUT(平均值)——由箭头连接
每个算法分解成输入、处理和输出——这里,求三个分数的平均值
词汇表 训练
英文 中文 拼音
algorithm 算法 suàn fǎ
input 输入 shū rù
processing 处理 chǔ lǐ
output 输出 shū chū
7.4

有效性检查与准确性检查

当数据被输入时,你检查它以减少错误。

验证(validation)检查数据是合理的并遵循规则。它不能检查数据是真的,只能检查它是被允许的。

验证检查 它检查什么
范围检查(range check) 值在一个最低和最高允许值之间
长度检查(length check) 字符的数目被允许(例如一个密码 ≥ 8)
类型检查(type check) 数据是正确的类型(例如一个数,而不是字母)
存在性检查(presence check) 某样东西实际被输入(没有留空)
格式检查(format check) 数据在正确的模式中(例如一个日期作为 dd/mm/yyyy)
校验码(check digit) 一个额外的数字确认一个数被正确地输入

核实(verification)检查数据被正确地复制或输入(键入它时没有错误)。两种方法:

  • 目视检查(visual check)——一个人把键入的数据与原始比较;
  • 双重输入(double entry)——数据被输入两次,而两个副本被比较。
词汇表 训练
英文 中文 拼音
validation 验证 yàn zhèng
range check 范围检查 fàn wéi jiǎn chá
length check 长度检查 cháng dù jiǎn chá
type check 类型检查 lèi xíng jiǎn chá
presence check 存在性检查 cún zài xìng jiǎn chá
format check 格式检查 gé shì jiǎn chá
check digit 校验码 jiào yàn mǎ
verification 核实 hé shí
visual check 目视检查 mù shì jiǎn chá
double entry 双重输入 shuāng chóng shū rù
7.5

跟踪表

一个追踪表(trace table)一步步地记录一个算法运行时每个变量的值。它帮助你:

一个追踪表,带列 count、total、output
一个追踪表在程序运行时记录每个变量的值
  • 检查一个算法正确地工作;
  • 通过用给定的数据跟随它来算出一个算法做什么

例子:用输入 5 追踪这个算法。

INPUT n
total ← 0
FOR i ← 1 TO n
    total ← total + i
NEXT i
OUTPUT total
i total OUTPUT
1 1
2 3
3 6
4 10
5 15 15

追踪显示算法把 1 到 n 相加。用输入 5,输出是 15。

例题。 追踪这个算法并给出输出。

x ← 20
count ← 0
WHILE x > 1
    x ← x DIV 2
    count ← count + 1
ENDWHILE
OUTPUT count

DIV 只取除法的整数部分。每循环一次写一行:x 变成 10(count 为 1),然后 5(count 为 2),然后 2(count 为 3),然后 1(count 为 4)。此时 x > 1 为假,所以循环结束,输出是 4。有两个习惯能保住这些分:在每次循环之前而不是之后检验条件;以及每一次循环都新写一行 - 想把这些值记在脑子里,正是追踪出错的原因。

探索

A trace table

Step through the loop and fill in the trace table, one row per pass.

词汇表 训练
英文 中文 拼音
trace table 追踪表 zhuī zōng biǎo
7.6

测试数据

测试数据(test data)是你用来测试一个程序的数据。有四种你必须知道的类型。

类型 意义 例子(年龄 0–120 允许)
正常数据(normal) 应当被接受的合理数据 25
异常数据(abnormal) 应当被拒绝的错误数据 -4 或 "cat"
极端数据(extreme) 仍被允许的最大和最小值 0 和 120
边界数据(boundary) 一个限制每一侧的值(一个允许,一个不) 120 和 121
词汇表 训练
英文 中文 拼音
normal 正常数据 zhèng cháng shù jù
abnormal 异常数据 yì cháng shù jù
extreme 极端数据 jí duān shù jù
boundary 边界数据 biān jiè shù jù
7.7

标准解题方法

你必须知道这些常见的算法。

Linear search

一个线性查找(linear search)逐个检查一个列表中的每个项,直到它找到它想要的值或到达末端。

found ← FALSE
FOR i ← 0 TO 9
    IF list[i] = searchValue THEN
        found ← TRUE
    ENDIF
NEXT i
OUTPUT found
一个八个数的列表从左到右被扫描,搜索 5;前四个不匹配,而第五个被找到
线性查找从开始依次检查每个项,直到它找到值

Bubble sort

一个冒泡排序(bubble sort)把一个列表排序。它比较每对并排的项,若它们顺序错误就交换它们。它重复这个直到不再需要交换。

FOR i ← 0 TO 8
    IF list[i] > list[i + 1] THEN
        temp ← list[i]
        list[i] ← list[i + 1]
        list[i + 1] ← temp
    ENDIF
NEXT i
一个列表,第一对 5 和 2 顺序错误,显示交换成 2 和 5,带一个对每对重复的注释
冒泡排序比较每对并排的对,若它们顺序错误就交换它们,重复直到排序好

Totalling and counting

  • 求和(totalling)——不断把值加到一个运行中的总数(total ← total + value)。
  • 计数(counting)——每次某样东西发生时给一个计数器加 1(count ← count + 1)。

Maximum, minimum and average

  • 要求最大值(maximum):保留到目前为止见过的最大值。
  • 要求最小值(minimum):保留到目前为止见过的最小值。
  • 要求平均值(average):把总数除以有多少个值。
total ← 0
FOR i ← 0 TO 9
    total ← total + list[i]
NEXT i
average ← total / 10
OUTPUT average
词汇表 训练
英文 中文 拼音
linear search 线性查找 xiàn xìng chá zhǎo
bubble sort 冒泡排序 mào pào pái xù
totalling 求和 qiú hé
counting 计数 jì shù
maximum 最大值 zuì dà zhí
minimum 最小值 zuì xiǎo zhí
average 平均值 píng jūn zhí
7.8

考试技巧

  • 学习四个生命周期阶段:分析 → 设计 → 编码 → 测试。抽象只保留重要的细节;分解把一个问题分成更小的部分。
  • 验证检查数据是合理的(范围、长度、类型、存在性、格式检查);核实检查它被正确地复制(一个目视检查或双重输入)。
  • 学习四种测试数据类型:正常(被接受)、异常(被拒绝)、极端(仍被允许的最大/最小)、边界(一个限制两侧的值)。
  • 要算出一个算法做什么,填一个追踪表——写下每个变量在每一步的值。
  • 知道标准算法:线性查找(依次检查每个项)和冒泡排序(交换并排的对直到不需要交换)。

本主题的互动课程

逐步学习,并即时检测练习。

IGCSE 计算机科学历年真题

IGCSE 计算机科学的更多主题

登录或创建账号

IGCSE, A-Level & AP