📚 Problem-Solving Methods in Programming | 程序设计中的问题求解方法
Programming is not merely about writing code — it is fundamentally an exercise in structured problem solving. A professional programmer must first understand a problem thoroughly, then design a logical plan, and finally translate that plan into a precise sequence of instructions that a computer can execute. This article examines the core problem-solving methods taught in the CIE A-Level Computer Science syllabus, including abstraction, decomposition, algorithm design, and the evaluation of solutions.
程序设计不仅仅是编写代码——它本质上是一种结构化的问题求解训练。专业的程序员必须首先深入理解问题,然后设计出逻辑方案,最后将该方案转化为计算机能够执行的精确指令序列。本文系统梳理 CIE A-Level 计算机科学考纲中涉及的核心问题求解方法,包括抽象、分解、算法设计以及解决方案的评估。
1. What Is an Algorithm? | 什么是算法?
An algorithm is a finite, unambiguous, step-by-step procedure for solving a specific problem or completing a given task. For an algorithm to be valid, it must terminate after a finite number of steps, and every step must be precisely defined without ambiguity. Algorithms can be expressed in natural language, pseudocode, flowcharts, or a high-level programming language.
算法是为解决特定问题或完成给定任务而设计的有限、无歧义、逐步执行的过程。一个有效的算法必须在有限步骤内终止,且每一步都要精确定义、不可产生歧义。算法可以用自然语言、伪代码、流程图或高级程序设计语言来表达。
Key Properties of an Algorithm | 算法的关键属性:Finiteness(有限性) → Definiteness(确定性) → Input(输入) → Output(输出) → Effectiveness(有效性)
Finiteness: The algorithm must always terminate after a finite number of steps.
有限性:算法必须在有限步数之后终止。
Definiteness: Each step must be precise and unambiguous.
确定性:每一步都必须精确且无歧义。
Input/Output: An algorithm may take zero or more inputs and must produce at least one output.
输入/输出:算法可接受零个或多个输入,且必须产生至少一个输出。
Effectiveness: Every operation must be basic enough to be performed exactly in a finite amount of time.
有效性:每个操作都必须足够基础,能够在有限时间内精确完成。
2. Decomposition | 分解
Decomposition is the process of breaking a complex problem down into smaller, more manageable sub-problems. Each sub-problem can then be solved independently, and the solutions are combined to form the complete answer. In programming, decomposition corresponds to modularisation — dividing a program into procedures, functions, and modules, each responsible for a specific task.
分解是将复杂问题拆分为更小、更易管理的子问题的过程。每个子问题都可以独立求解,随后将各子问题的解决方案合并以得到完整答案。在程序设计中,分解对应着模块化——将程序划分为过程、函数和模块,每个部分负责一项特定任务。
Reduces complexity: Smaller problems are easier to understand and solve.
降低复杂性:较小的问题更容易理解和求解。
Supports teamwork: Different developers can work on different modules simultaneously.
支持团队协作:不同的开发人员可以同时处理不同的模块。
Facilitates testing: Each module can be tested and verified independently.
便于测试:每个模块可以独立测试和验证。
Improves maintainability: Changes to one module do not affect the entire system.
提高可维护性:对某一模块的修改不会影响整个系统。
For example, writing a payroll system can be decomposed into sub-problems such as calculating working hours, computing tax deductions, generating payslips, and storing employee records.
例如,编写薪资管理系统可分解为以下子问题:计算工作时长、计算税收扣除、生成工资单以及存储员工记录。
3. Abstraction | 抽象
Abstraction is the process of removing unnecessary details and focusing only on the essential features relevant to the problem being solved. In programming, abstraction allows a programmer to model real-world objects as data structures and operations, ignoring irrelevant physical details. For instance, when modelling a car in a racing game, the programmer considers speed, position and fuel — not the colour of the upholstery or the tyre brand.
抽象是去除无关紧要的细节、只关注与当前问题相关的本质特征的过程。在程序设计中,抽象使程序员能够将现实世界对象建模为数据结构与操作,忽略无关的物理细节。例如,在赛车游戏中为汽车建模时,程序员考虑速度、位置和燃油——而忽略座椅颜色或轮胎品牌。
Abstraction removes the irrelevant · 抽象去除无关信息; Generalisation groups similarities · 概括归并相似特征
Abstraction is closely related to generalisation. While abstraction strips away details, generalisation identifies common patterns among several specific cases to form a reusable model. Both techniques reduce the cognitive load on the programmer and enable the construction of large, complex software systems.
抽象与概括密切相关。抽象剥离细节,而概括则是从多个具体情形中识别共同模式以形成可复用的模型。两种技术都能降低程序员的认知负担,并支持大型复杂软件系统的构建。
4. Representing Algorithms | 算法的表示方式
Algorithms can be represented in several standard forms, each with distinct advantages. The CIE syllabus requires candidates to be familiar with structured English, pseudocode, and flowcharts.
算法可以用若干标准形式来表示,每种形式都有其独特的优势。CIE 考纲要求考生熟悉结构化英语、伪代码和流程图。
4.1 Structured English | 结构化英语
Structured English uses a limited vocabulary of plain English words combined with programming constructs such as IF, THEN, ELSE, WHILE, and REPEAT. It is useful for early-stage design discussions with non-technical stakeholders.
结构化英语使用有限的自然英语词汇,结合 IF、THEN、ELSE、WHILE、REPEAT 等程序构造。它在与不懂技术的利益相关者进行早期设计讨论时非常有用。
4.2 Pseudocode | 伪代码
Pseudocode resembles a high-level programming language but omits strict syntax rules. It allows the programmer to focus on logic rather than language-specific details. CIE uses a standard pseudocode style in examination papers, which candidates must learn to read and write accurately.
伪代码类似于高级程序设计语言,但省略严格的语法规则。它使程序员专注于逻辑而不是特定语言的细节。CIE 在考试试卷中使用标准的伪代码风格,考生必须学会准确阅读和书写。
4.3 Flowcharts | 流程图
Flowcharts use standard symbols — terminator (oval), process (rectangle), decision (diamond), input/output (parallelogram) — to depict the flow of control graphically. Flowcharts are excellent for visual learners and for representing small- to medium-sized algorithms.
流程图使用标准符号——终止符(椭圆)、处理框(矩形)、判断框(菱形)、输入/输出框(平行四边形)——以图形方式描绘控制流。流程图非常适合视觉学习者,也适用于中小型算法的表示。
5. Sequence, Selection and Iteration | 顺序、选择与迭代
Every executable algorithm in structured programming is built from three fundamental control structures: sequence, selection, and iteration. Understanding these constructs is essential for writing clear and correct programs.
结构化程序设计中的每个可执行算法都由三种基本控制结构构建而成:顺序、选择和迭代。理解这些结构对于编写清晰且正确的程序至关重要。
Sequence: Statements are executed one after another in the order they appear. For example: x ← 3; y ← 4; z ← x + y.
顺序:语句按其出现的顺序依次执行。例如:x ← 3;y ← 4;z ← x + y。
Selection: A condition is tested, and different statements are executed depending on the Boolean result. IF … THEN … ELSE … is the classic construct.
选择:测试一个条件,根据布尔结果执行不同的语句。IF … THEN … ELSE … 是经典的构造。
Iteration: A block of statements is repeated either a fixed number of times (FOR loop) or while a condition remains true (WHILE loop, REPEAT UNTIL loop).
迭代:语句块重复执行,可以是固定次数(FOR 循环),也可以是条件为真时持续执行(WHILE 循环、REPEAT UNTIL 循环)。
Structured programming theorem: Any computable function can be expressed using only these three constructs. 结构化程序定理:任何可计算函数都可通过这三种结构表达。
6. Divide and Conquer | 分治法
Divide and conquer is a powerful problem-solving paradigm that breaks a problem into several smaller sub-problems of the same type, recursively solves each sub-problem, and then combines their solutions to solve the original problem. Classic examples include merge sort, quick sort, and binary search.
分治法是一种强大的问题求解范式:将一个问题分解为多个相同类型的较小子问题,递归地求解每个子问题,再将它们的解合并以解决原问题。典型例子包括归并排序、快速排序和二分查找。
Divide the problem → Conquer the sub-problems recursively → Combine the solutions
分解原问题 → 递归求解子问题 → 合并子问题的解
tance, the recurrence for binary search is T(n) = T(n/2) + O(1), which solves to O(log n).
分析:许多分治算法的运行时间可以用递推关系表达。例如,二分查找的递推式为 T(n) = T(n/2) + O(1),解得时间复杂度为 O(log n)。
| Algorithm | 算法 | Divide step | 分解 | Combine step | 合并 |
| Merge Sort | 归并排序 | Split array in half | 将数组对半拆分 | Merge sorted halves | 合并有序的一半 |
| Binary Search | 二分查找 | Discard one half | 舍弃一半 | No combine needed | 无需合并 |
| Quick Sort | 快速排序 | Partition by pivot | 按枢轴划分 | Arrays already sorted | 数组已有序 |
7. Backtracking | 回溯法
Backtracking is an algorithmic technique for solving problems incrementally by attempting to build a solution piece by piece and removing ("backtracking") partial solutions that fail to satisfy the constraints. It is commonly applied to constraint satisfaction problems such as the N-Queens puzzle, Sudoku, and maze solving.
回溯法是一种增量式构建解决方案的算法技术:逐步构建解,并在部分解无法满足约束条件时移除(即"回溯")这些部分解。它通常应用于约束满足问题,例如 N 皇后问题、数独和迷宫求解。
At each step, a candidate move is chosen and the algorithm checks whether the move is valid under the constraints.
在每一步中,选择一个候选移动,并检查该移动在约束下是否有效。
If valid, the algorithm moves recursively to the next step.
如果有效,则递归进入下一步。
If no valid move exists, the algorithm backtracks — it undoes the previous move and tries an alternative.
如果不存在有效移动,算法回溯——撤销上一步并尝试其他选择。
The technique guarantees finding a solution if one exists, at the cost of potential exponential time in the worst case.
该技术保证在存在解时一定能找到解,但最坏情况下可能需要指数级时间。
For the 4-Queens problem, the algorithm places a queen in the first row, then tries positions in the second row, backtracking whenever two queens threaten each other, until all four queens are safely placed.
对于 4 皇后问题,算法在第一行放置一个皇后,然后尝试第二行的各个位置,只要两个皇后互相威胁就回溯,直到四个皇后安全放置完毕。
8. Heuristic Methods | 启发式方法
A heuristic is a practical, experience-based technique that seeks a good-enough solution when an optimal or exact solution is impractical due to time or computational constraints. Unlike exhaustive search, heuristics trade completeness for speed and efficiency.
启发式是一种基于经验的实用技术,当由于时间或计算限制而无法获得最优或精确解时,它寻求一个足够好的解。与穷举搜索不同,启发式用完备性换取速度和效率。
Greedy algorithms: Make the locally optimal choice at each step, hoping to reach a global optimum. Example: Dijkstra's shortest-path algorithm.
贪心算法:在每一步做出局部最优选择,希望达到全局最优。示例:Dijkstra 最短路径算法。
Hill climbing: Move in the direction of increasing improvement until no better neighbour exists. It may get stuck at local optima.
爬山法:朝改进增大的方向移动,直到不存在更好的邻居。它可能陷入局部最优。
Genetic algorithms: Use selection, crossover and mutation to evolve solutions over generations.
遗传算法:使用选择、交叉和变异在迭代中进化求解。
Heuristic methods are widely used in artificial intelligence, route planning, scheduling, and game-playing programs where the search space is too large to explore exhaustively.
启发式方法广泛用于人工智能、路径规划、调度和博弈程序中,因为这类问题的搜索空间大得无法穷举。
9. Evaluating a Solution | 评估解决方案
Once an algorithm has been designed, implemented, and tested, it must be evaluated critically. Evaluation criteria include correctness, efficiency, readability, and maintainability.
算法设计、实现和测试完成后,必须对其进行批判性评估。评估标准包括正确性、效率、可读性和可维护性。
| Criterion | 标准 | Question to ask | 问询要点 |
| Correctness | 正确性 | Does it produce the expected output for all valid inputs? | 对所有合法输入是否产生预期输出? |
| Time efficiency | 时间效率 | How does the running time grow as input size increases? | 运行时间如何随输入规模增长? |
| Space efficiency | 空间效率 | How much memory does the algorithm require? | 算法需要多少内存? |
| Readability | 可读性 | Can another programmer understand the code easily? | 其他程序员能否轻松理解代码? |
| Maintainability | 可维护性 | How easy is it to modify the code when requirements change? | 当需求变化时修改代码的难度如何? |
Complexity analysis uses Big-O notation to express the upper bound of an algorithm's growth rate. A linear search runs in O(n), while a binary search runs in O(log n). Sorting algorithms range from O(n²) (bubble sort) to O(n log n) (merge sort, quick sort average case).
复杂度分析使用大 O 符号来表示算法增长率的上界。线性查找的时间复杂度为 O(n),而二分查找为 O(log n)。排序算法从 O(n²)(冒泡排序)到 O(n log n)(归并排序、快速排序平均情况)不等。
10. Practical Problem-Solving Process | 实际问题求解流程
A systematic approach to programming problems integrates all the methods discussed above. The CIE syllabus encourages a disciplined, staged workflow.
系统地解决程序设计问题需要整合上述所有方法。CIE 考纲鼓励采用有纪律的、分阶段的工作流程。
Analyse the problem statement and identify inputs, outputs, and constraints.
分析问题陈述,明确输入、输出和约束条件。
Decompose the problem into sub-problems and apply abstraction to focus on essentials.
将问题分解为子问题,并运用抽象聚焦本质。
Design the algorithm using pseudocode, flowcharts, or structured English.
使用伪代码、流程图或结构化英语设计算法。
Choose an appropriate algorithm paradigm — divide and conquer, backtracking, heuristic, or brute force — based on problem size and constraints.
根据问题规模和约束条件选择合适的算法范式——分治法、回溯法、启发式或蛮力法。
Implement the algorithm in a high-level language, following principles of clear naming and modularity.
用高级语言实现算法,遵循清晰的命名和模块化原则。
Test with normal, boundary, and invalid inputs; debug as necessary.
使用正常、边界和无效输入进行测试;必要时调试。
Evaluate the solution for correctness, efficiency, and maintainability, and optimise if required.
评估方案的正确性、效率和可维护性,必要时进行优化。
This structured process ensures that a programmer builds robust, efficient and understandable software, regardless of the complexity of the original problem.
这一结构化流程确保程序员能够构建健壮、高效且易于理解的软件,无论原始问题多么复杂。
Published by TutorHao | Computer Science Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)

或截图后用「扫一扫 → 相册」选择本图