📚 7S Homework Unit 6: Algorithm Design and Problem Solving | 7S 作业第六单元:算法设计与问题求解
This article covers the core ideas in Unit 6 of 7S Computer Science homework. It focuses on algorithm design, including decomposition, pseudocode, flowcharts, control structures, searching and sorting algorithms, efficiency, and debugging. These skills are essential for both theory papers and practical coding tasks.
本文涵盖 7S 计算机科学作业第六单元的核心内容,重点讲解算法设计,包括分解、伪代码、流程图、控制结构、查找与排序算法、效率分析和调试。这些技能对理论考试和实际编程任务都至关重要。
1. Understanding the Problem | 理解问题
Before writing any code, a programmer must define the problem precisely. A clear problem statement includes the inputs, the required outputs, and any constraints such as time limits or data range.
在编写任何代码之前,程序员必须准确定义问题。清晰的问题陈述包括输入、所需输出以及时间限制或数据范围等约束条件。
For example, in a login system the inputs are a username and password, the output is a success or failure message, and a constraint is that passwords must be hashed and stored securely.
例如,在登录系统中,输入是用户名和密码,输出是成功或失败消息,约束条件是密码必须经过哈希处理并安全存储。
2. Decomposition and Abstraction | 分解与抽象
Decomposition means breaking a large problem into smaller, more manageable sub-problems. Each sub-problem can then be solved independently and combined to form the final solution.
分解是指将一个大问题拆分成更小、更易管理的子问题。每个子问题可以独立解决,然后组合成最终解决方案。
Abstraction means hiding unnecessary detail and focusing only on the important features of a problem. For instance, when designing a car simulation, we do not model every atom of the car; we only keep attributes such as speed, position, and fuel level.
抽象是指隐藏不必要的细节,只关注问题的重要特征。例如,在设计汽车模拟时,我们不会对汽车的每个原子建模;我们只保留速度、位置和油量等属性。
3. Algorithm Basics and Pseudocode | 算法基础与伪代码
An algorithm is a finite sequence of well-defined steps that solves a problem. It must be clear, unambiguous, and eventually terminate.
算法是解决某个问题的有限且定义明确的步骤序列。它必须清晰、无歧义,并且最终能够终止。
Pseudocode is a way to describe algorithms using plain English and simple programming-like notation. It is not run on a computer, but it helps programmers plan logic before coding.
伪代码使用简明英语和类似编程的符号来描述算法。它不在计算机上运行,但能帮助程序员在编写代码前规划逻辑。
INPUT temperature
IF temperature > 30 THEN
OUTPUT "It is hot"
ELSE
OUTPUT "It is comfortable"
ENDIF
以上伪代码展示了条件判断:如果温度大于 30 则输出 "It is hot",否则输出 "It is comfortable"。
Pseudocode can also express loops. The following example prints the numbers 1 to 10.
伪代码也可以表示循环。以下示例输出数字 1 到 10。
FOR i ← 1 TO 10
OUTPUT i
ENDFOR
该循环从 i = 1 开始,每次输出 i,直到 i = 10 结束。
4. Flowcharts and Their Symbols | 流程图及其符号
Flowcharts are visual diagrams that represent the flow of an algorithm. Standard symbols include ovals for start and end, rectangles for processes, diamonds for decisions, and parallelograms for input and output.
流程图是表示算法流程的可视化图表。标准符号包括:椭圆表示开始和结束,矩形表示处理过程,菱形表示判断,平行四边形表示输入和输出。
Arrows connect the symbols to show the order of execution. A well-drawn flowchart makes complex logic easier to understand and debug.
箭头连接各符号以显示执行顺序。绘制良好的流程图可以使复杂逻辑更易理解和调试。
- Terminal (oval): Start or End. 开始或结束。
- Process (rectangle): Calculation or assignment. 计算或赋值。
- Decision (diamond): Yes/No condition. 是/否判断。
- Input/Output (parallelogram): Read or print data. 读取或输出数据。
5. Sequence, Selection, Iteration | 顺序、选择与迭代
Three fundamental control structures are used in all structured programming: sequence, selection, and iteration. Sequence means executing statements one after another in order.
所有结构化编程中都使用三种基本控制结构:顺序、选择和迭代。顺序是指按照顺序逐条执行语句。
Selection allows the program to choose between different paths. This is implemented with IF, IF-ELSE, or CASE statements. For example, a grade calculator chooses an output based on a score.
选择允许程序在不同路径之间做出选择。这通过 IF、IF-ELSE 或 CASE 语句实现。例如,成绩计算器根据分数选择输出。
Nested selection places one IF inside another. For example, a login checker can first verify the username, then check the password only if the username exists.
嵌套选择将一个 IF 放在另一个 IF 内部。例如,登录检查器可以先验证用户名,只有用户名存在时才检查密码。
Iteration repeats a block of code. There are definite loops such as FOR, which run a known number of times, and indefinite loops such as WHILE, which run while a condition is true.
迭代使一段代码重复执行。确定循环(如 FOR)运行已知次数,而不确定循环(如 WHILE)在条件为真时持续运行。
6. Variables, Constants and Data Types | 变量、常量与数据类型
A variable is a named storage location whose value can change during program execution. A constant is similar, but its value cannot be changed after it is set once.
变量是一个命名的存储位置,其值在程序执行期间可以改变。常量类似,但其值一旦设置就不能再改变。
Common data types include integer, real/float, Boolean, character, and string. Choosing the correct data type saves memory and prevents type errors.
常见数据类型包括整数、实数/浮点数、布尔值、字符和字符串。选择正确的数据类型可以节省内存并防止类型错误。
| Data Type 数据类型 | Example 示例 | Use 用途 |
|---|---|---|
| Integer | 42, -7 | Whole numbers 整数 |
| Real/Float | 3.14, -0.01 | Decimal numbers 小数 |
| Boolean | 广告 / Sponsored 正版包邮 TI-nspire CX Guide for IBDP & AP 书店 计算机与网络 书籍 畅想畅销书 计算机 备考教辅 · 正版现货 · 与本文内容相关 查看详情 / View on Taobao参考价 ¥115.0
更多咨询请联系16621398022(同微信) 备考资讯每日更新,关注不错过 考纲变化 · 科目分析 · 学习方法 ![]() 在微信里长按二维码识别关注 或截图后用「扫一扫 → 相册」选择本图 公众号:tutorhao |
