Skip to content

Commit e5a9d3d

Browse files
committed
Update algorithm examples
1 parent 5961fb9 commit e5a9d3d

8 files changed

Lines changed: 330 additions & 845 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- 添加对 `algorithms` 宏包的支持。
13+
1014
### Changed
1115

1216
- 允许 `\statement` 命令使用可选参数 `[file=foo.pdf]` 插入扫描后的 PDF([#506](https://github.com/ustctug/ustcthesis/issues/506))。

chapters/floats.tex

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -77,26 +77,29 @@ \section{插图}
7777

7878
\section{算法环境}
7979

80-
模板中使用 \pkg{algorithm2e} 宏包实现算法环境。关于该宏包的具体用法,
81-
请阅读宏包的官方文档。
80+
模板中使用 \pkg{algorithms} 宏包实现算法环境。
81+
关于该宏包的具体用法,请阅读宏包的官方文档。
82+
用户也可以使用 \pkg{algorithmicx} 和 \pkg{algorithm2e} 等宏包。
8283

8384
\begin{algorithm}
84-
\SetAlgoLined
85-
\KwData{this text}
86-
\KwResult{how to write algorithm with \LaTeX2e }
87-
88-
initialization\;
89-
\While{not at end of this document}{
90-
read current\;
91-
\eIf{understand}{
92-
go to next section\;
93-
current section becomes this one\;
94-
}{
95-
go back to the beginning of current section\;
96-
}
97-
}
98-
\caption{算法示例1}
99-
\label{algo:algorithm1}
85+
\caption{欧几里得算法}
86+
\label{alg:euclid}
87+
\begin{algorithmic}
88+
\renewcommand{\algorithmicrequire}{\textbf{输入:}}
89+
\renewcommand{\algorithmicensure}{\textbf{输出:}}
90+
\REQUIRE 两个非负整数 $a$$b$
91+
\ENSURE $a$$b$ 的最大公约数 $\gcd(a, b)$
92+
93+
\WHILE{$b \neq 0$}
94+
\IF{$a > b$}
95+
\STATE $a \gets a - b$
96+
\ELSE
97+
\STATE $b \gets b - a$
98+
\ENDIF
99+
\ENDWHILE
100+
101+
\RETURN $a$
102+
\end{algorithmic}
100103
\end{algorithm}
101104

102105
注意,我们可以在论文中插入算法,但是插入大段的代码是愚蠢的。

test/testfiles-crossref/package-algorithm2e.tex

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,25 @@ \chapter{标题}
2020

2121
\TIMO
2222
\begin{algorithm}
23-
\caption{计算 $y = x^n$}
24-
\label{alg1}
2523
\SetKwInOut{Input}{输入}
2624
\SetKwInOut{Output}{输出}
2725

28-
\Input{$n \geq 0 \vee x \neq 0$}
29-
\Output{$y = x^n$}
26+
\Input{两个非负整数 $a$$b$}
27+
\Output{$a$$b$ 的最大公约数 $\gcd(a, b)$}
28+
\BlankLine
3029

31-
$y \leftarrow 1$\;
32-
\eIf{$n < 0$}{
33-
$X \leftarrow 1 / x$\;
34-
$N \leftarrow -n$\;
35-
}{
36-
$X \leftarrow x$\;
37-
$N \leftarrow n$\;
38-
}
39-
40-
\While{$N \neq 0$}{
41-
\eIf{$N$ is even}{
42-
$X \leftarrow X \times X$\;
43-
$N \leftarrow N / 2$\;
30+
\While{$b \neq 0$}{
31+
\eIf{$a > b$}{
32+
$a \gets a - b$\;
4433
}{
45-
% \tcp*{$N$ is odd}
46-
$y \leftarrow y \times X$\;
47-
$N \leftarrow N - 1$\;
34+
$b \gets b - a$\;
4835
}
4936
}
37+
38+
\Return $a$\;
39+
40+
\caption{欧几里得算法}
41+
\label{alg:euclid}
5042
\end{algorithm}
5143

5244
\clearpage

0 commit comments

Comments
 (0)