算法中显示行号_Latex写伪代码算法(九)

1638c04237be1c753859737ed58decb1.png

一、algorithm包

1)首先在导言区加入语句:

\usepackage{algorithm}  \usepackage{algorithmic}

2)

\section{Algorithm}\subsection{Algorithm1}\begin{algorithm}[!h]  \caption{PARTITION$(A,p,r)$}%算法标题  \begin{algorithmic}[1]%一行一个标行号    \STATE $i=p$    \FOR{$j=p$ to $r$}    \IF{$A[j]<=0$}    \STATE $swap(A[i],A[j])$    \STATE $i=i+1$    \ENDIF    \ENDFOR  \end{algorithmic}\end{algorithm}

输出结果:

97e33a967ff1233895c93bdc18e73e39.png

介绍下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{
    
  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值