§0 相关基础知识回顾
§0.1 数据与数据结构
- 线性结构(linear structure,也称线性表,如数组、链表、栈、队列等)
- 非线性结构(nonlinear structure,如树结构、群结构)
§0.2 C++基础知识回顾
§0.2.1 类的初始化
class Point {
public:
Point(int, int);
/* omit */
private:
int x, y;
};
// Regular initialization
Point::Point(int x, int y) {
this->x = x;
this->y = y;
}
// If want to initialize const
Point::Point(int x, int y) : x(x), y(y) {
/* initialize other variable */
}
§0.2.2 输入输出的重载
class Point {
public:
Point(int, int);
/* omit */
// Overloading of input and output
friend istream& operator>>(istream& in, Point& p);
friend ostream& operator<<(ostream& out, Point& p);
private:
int x, y;
};
// Input overloading is in the same way
ostream& Point::operator<<(ostream& out, Point& p) {
out << "P(" << p.getX() << "," << p.getY() << ")" << endl;
return out;
}
§0.2.3 C++中的继承
- 子类继承的格式为:
class Subclass : public Superclass
,即在冒号后选择继承方式(如public
)再选择基类。 - 子类构造函数的格式为:
Subclass(/*argument list*/) : Superclass(/*argument list*/)
,即在冒号后调用基类的构造函数。
class Animal {
public:
Animal(int weight) {
/* omit */
}
/* omit */
};
class Fish : public Animal {
public:
Fish(int weight, int length) : Animal(weight) {
/* omit */
}
/* omit */
};
§0.3 算法概览
§0.3.1 算法的定义
- 有输入
- 有输出
- 确定性(Definiteness)
- 有穷性(Finiteness)
- 可行性(Effectiveness)
§0.3.2 算法的性能标准
算法是解决特定问题求解步骤的描述,在计算机中表现为指令的有限序列,并且每条指令表示一个或多个操作。
- 正确性(Correctness)
- 可读性(Readability)
- 健壮性(Robustness)
- 效率(Efficiency)
§0.3.3 时间复杂度度量
- 定义
在进行算法分析时,语句总的执行次数T(n)是关于问题规模n的函数,进而分析T(n)随n的变化情况,并确定T(n)的数量级。
算法的时间复杂度,记作:T(n)=O(f(n))。表示随问题规模n的增大,算法执行时间的增长率和f(n)的增长率相同,称作算法的渐近时间复杂度,简称为时间复杂度。其中f(n)是问题规模n的某个函数。用大写O()来体现算法时间复杂度的记法,称之为大O记法。 - 度量方式
语句 | 程序步数 |
---|---|
注释 | 0 |
声明语句 | 0 |
不包含函数调用的表达式 | 1 |
包含函数调用的表达式 | 包括分配给函数调用的程序步数 |
赋值语句 | 与表达式同理,若赋值语句中的变量是数组或字符串,则程序步数等于变量的体积加上表达式的程序步数 |
循环语句 | 需要考虑循环控制部分 |
条件分支语句 | 判断的表达式程序步数+大括号内的步数 |
转移语句(continue, break, return) | 除return外为1,return为后面跟的表达式的程序步数 |
- 常见阶数
常数阶O(1),对数阶O(logn),线性阶O(n),平方阶O(n²),指数阶O(2^n)等。
§0.3.4 空间复杂度度量
略