LaTeX/Algorithms 伪代码

来源:互联网 发布:淘宝比较好的模型店 编辑:程序博客网 时间:2024/05/16 23:43

转自:http://hustsxh.is-programmer.com/posts/38801.html


algorithmic和algorithmicx

介绍下algorithmic和algorithmicx,这两个包很像,很多命令都是一样的,只是algorithmic的命令都是大写,algorithmicx的命令都是首字母大写,其他小写(EndFor两个大写)。下面是algorithmic的基本命令

\STATE <text>

\IF{<condition>} \STATE{<text>} \ENDIF

\FOR{<condition>} \STATE{<text>} \ENDFOR

\FOR{<condition> \TO <condition> } \STATE{<text>} \ENDFOR

\FORALL{<condition>} \STATE{<text>} \ENDFOR

\WHILE{<condition>} \STATE{<text>} \ENDWHILE

\REPEAT \STATE{<text>} \UNTIL{<condition>}

\LOOP \STATE{<text>} \ENDLOOP

\REQUIRE <text>

\ENSURE <text>

\RETURN <text>

\PRINT <text>

\COMMENT{<text>}

\AND\OR\XOR\NOT\TO\TRUE\FALSE

对比看一下,下面是algorithmicx包的基本命令

\State <text>

\If{<condition>} <text> \EndIf

\If{<condition>} <text> \Else <text> \EndIf

\If{<condition>} <text> \ElsIf{<condition> <text> \Else <text> \EndIf

\For{<condition>} <text> \EndFor

\ForAll{<condition><text> \EndFor

\While{<condition>} <text> \EndWhile

\Repeat <text> \Until{<condition>}

\Loop <text> \EndLoop

\Require <text>

\Ensure <text>

\Function{<name>}{<params>} <body> \EndFunction

\State \Return <text>

\Comment{<text>}

另外,还有3个修改algorithm标签,require标签,ensure标签显示的三个命令:

\floatname{algorithm}{算法}

\renewcommand{\algorithmicrequire}{\textbf{输入:}} 

\renewcommand{\algorithmicensure}{\textbf{输出:}}

algorithmicx例子

正好学着用algorithmicx写了下归并排序求逆序数,代码如下

 

?
归并算法求逆序数
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
\documentclass[11pt]{article}
\usepackage{CJK}
\usepackage[top=2cm, bottom=2cm, left=2cm, right=2cm]{geometry}
\usepackage{algorithm}
\usepackage{algorithmicx}
\usepackage{algpseudocode}
\usepackage{amsmath}
 
\floatname{algorithm}{算法}
\renewcommand{\algorithmicrequire}{\textbf{输入:}}
\renewcommand{\algorithmicensure}{\textbf{输出:}}
 
\begin{document}
\begin{CJK*}{UTF8}{gkai}
    \begin{algorithm}
        \caption{用归并排序求逆序数}
        \begin{algorithmic}[1]%每行显示行号
            \Require$Array$数组,$n$数组大小
            \Ensure逆序数
            \Function{MergerSort}{$Array, left, right$}
                \State$result \gets0$
                \If{$left < right$}
                    \State$middle \gets(left + right) / 2$
                    \State$result \getsresult +$ \Call{MergerSort}{$Array, left, middle$}
                    \State$result \getsresult +$ \Call{MergerSort}{$Array, middle, right$}
                    \State$result \getsresult +$ \Call{Merger}{$Array,left,middle,right$}
                \EndIf
                \State\Return{$result$}
            \EndFunction
            \State
            \Function{Merger}{$Array, left, middle, right$}
                \State$i\getsleft$
                \State$j\getsmiddle$
                \State$k\gets0$
                \State$result \gets0$
                \While{$i<middle$\textbf{and} $j<right$}
                    \If{$Array[i]<Array[j]$}
                        \State$B[k++]\getsArray[i++]$
                    \Else
                        \State$B[k++] \getsArray[j++]$
                        \State$result \getsresult + (middle - i)$
                    \EndIf
                \EndWhile
                \While{$i<middle$}
                    \State$B[k++] \getsArray[i++]$
                \EndWhile
                \While{$j<right$}
                    \State$B[k++] \getsArray[j++]$
                \EndWhile
                \For{$i = 0 \tok-1$}
                    \State$Array[left + i] \getsB[i]$
                \EndFor
                \State\Return{$result$}
            \EndFunction
        \end{algorithmic}
    \end{algorithm}
\end{CJK*}
\end{document}

最后的排版结果如下


0 0
原创粉丝点击