LaTeX/Algorithms 伪代码

xhSong posted @ 2013年4月11日 16:43 in latex with tags latex algorithm2e Algorithm algorithmic algorithmicx , 156498 阅读

这段时间在上算法课,有些作业题目要求写出伪代码。正好这段时间在学习latex,于是就收集整理了下latex描写算法的包。latex下描写算法的包主要有algorithmic, algorithmicx和algorithm2e

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写了下归并排序求逆序数,代码如下

 

\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 \gets 0$
                \If {$left < right$}
                    \State $middle \gets (left + right) / 2$
                    \State $result \gets result +$ \Call{MergerSort}{$Array, left, middle$}
                    \State $result \gets result +$ \Call{MergerSort}{$Array, middle, right$}
                    \State $result \gets result +$ \Call{Merger}{$Array,left,middle,right$}
                \EndIf 
                \State \Return{$result$}
            \EndFunction
            \State 
            \Function{Merger}{$Array, left, middle, right$}
                \State $i\gets left$
                \State $j\gets middle$
                \State $k\gets 0$
                \State $result \gets 0$
                \While{$i<middle$ \textbf{and} $j<right$}
                    \If{$Array[i]<Array[j]$}
                        \State $B[k++]\gets Array[i++]$
                    \Else
                        \State $B[k++] \gets Array[j++]$
                        \State $result \gets result + (middle - i)$
                    \EndIf
                \EndWhile
                \While{$i<middle$}
                    \State $B[k++] \gets Array[i++]$
                \EndWhile
                \While{$j<right$}
                    \State $B[k++] \gets Array[j++]$
                \EndWhile
                \For{$i = 0 \to k-1$}
                    \State $Array[left + i] \gets B[i]$
                \EndFor
                \State \Return{$result$}
            \EndFunction
        \end{algorithmic}
    \end{algorithm}
\end{CJK*}
\end{document}

最后的排版结果如下

algorithm2e还没有学习,待以后再更新吧

Avatar_small
a 说:
2013年4月12日 09:47

越来越有牛人博客的味道啦~

Avatar_small
zyt 说:
2013年4月12日 09:47

有空学习学习~

Avatar_small
hk 说:
2013年10月30日 12:34

好文,搜algpseudocode的時候這篇排在第二頁。
It does help!

Avatar_small
Bolin 说:
2016年3月09日 18:13

学习了! Thanks!

Avatar_small
www.mcafee.com/activ 说:
2019年7月16日 01:20

I Like To Read Your Blogs Always because I found Your Blogs Interesting and I like Format..Keep Posting

Avatar_small
cleaning companies i 说:
2019年9月14日 16:16

If you are searching for a thorough cleaning organization in UAE, most likely, your research would end whenever you access the website. We provide you with a list associated with professional cleaning providers for any kind of cleaning you would like regardless of the property.

Avatar_small
wendywoodlaw.com 说:
2019年11月14日 00:49

nutritious diet will mean eating the perfect proportions and servings of each and every food. If you happen to eat usually protein healthy foods but not very many fruits and vegetables, the foods you eat isn't nutritious. Following the best portion sizes of each and every food cluster: 3-4 oz of (cartomancy deck measured) in protein.

Avatar_small
www.mcafee.com activ 说:
2019年11月30日 15:17

McAfee is well known and popular antivirus program that a user can download and activate at mcafee activate card.

Avatar_small
tomtom get started 说:
2020年2月28日 15:50

TomTom is a location technology specialist who uses unique technologies to navigate people to their destination with the help of GPS MAP.

Avatar_small
tomtom update 说:
2020年2月28日 15:51

The continuous TomTom update is available for latest advancement so that user can get safer driver over any of place.

Avatar_small
The continuous TomTo 说:
2020年2月28日 15:51

TomTom Home is the official update tool for TomTom.com, that includes several helpful things like the flexibility to simply install new maps and services, restore and backup your TomTom, receive free traffic alerts, and even use services that ordinarily need a wireless information association.

Avatar_small
tomtom.com/getstarte 说:
2020年2月28日 15:52

TomTom Home is the official update tool for TomTom.com, that includes several helpful things like the flexibility to simply install new maps and services, restore and backup your TomTom, receive free traffic alerts, and even use services that ordinarily need a wireless information association.

Avatar_small
I sell pittsburgh ho 说:
2020年3月19日 20:56

Start with thinking about your position. Are you willing to buy a property? How much is it possible to afford in the monthly mortgage loan payment? How significantly space do you want? What aspects of town can you like? When you answer these kinds of questions, produce a "To Do" list and commence doing everyday research concerning property.

Avatar_small
wanamassa reale stat 说:
2020年3月19日 20:56

The most important investments coming from all our life has to be Real House investment and it's going to be for sure the most appreciated possessions we will get on each of our entire lifestyle.

Avatar_small
north bama real esta 说:
2020年3月19日 20:57

A true estate occupation is every bit as popular between males and females as this specific career won't require just about any degree as well as certificate. There are lots of Men along with women agents out there who will be doing this specific job sometimes full-time as well as part-time.

Avatar_small
atlanta black busine 说:
2020年3月19日 20:57

Organization list are generally compiled along with updated regular with help of assorted sources including yellow internet pages directories, business cards, annual stories, business along with industry web directories, and mobile phone verification.

Avatar_small
freeze business rate 说:
2020年3月19日 20:58

Net Buisness, online marketing and writing a blog all get together effectively. In certain, you could add wording links for a blog blogposts. These backlinks will throw your web site viewer by way of your website for your current affiliate software.

Avatar_small
maid service dubai 说:
2020年4月28日 19:39

That piece is undoubtedly a favorite around my Halloween clothing collection collection. Different trends could possibly come in addition to go, but going to be a sexy The french language Maid is usually a costume staple that can make you be noticed from almost any Halloween herd. Don't make an effort bringing ones purse to you when people wear that seductive attire, because guys will likely be falling in excess of themselves into their attempts to obtain you a drink!

Avatar_small
house painting servi 说:
2020年4月28日 19:39

painter is very vital on the subject of decision doing. By by using, you can potentially have some sort of glimpse connected with what your own home or garden will be like. The colorings and products used into your program are things that resemble people that are you can buy. You may perhaps mix in addition to or simply click a color that is certainly already premixed from the program.

Avatar_small
maids in dubai 说:
2020年4月28日 19:40

While you hire your servant maid to clean up you house or office, you will be permitting her to view to your special belongings. Hence before selecting a cleaning service, you will need to ask specified questions to discover more on her foundation information plus personal testimonials. First element is this, you will need to inquire your family members, relatives and also acquaintances with regard to their personal experience together with the servant service personnel.

Avatar_small
painters in Dubai 说:
2020年4月28日 19:40

To be able to give the home a certain method of atmosphere, then insides paints are certainly important. The paints that you choose to choose have either ambiance or coolness to your rooms of your abode and if you are going to paint you or retain house artists; keep under consideration there presently exists many ideas so you might consider. These strategies include deciding on things for example right coloration, colors, pigments, schemes, colouring methods, together with other related areas.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter