algorithmicx(use algpseudocode as layout)学习记录
时间:2023-03-24
本文章向大家介绍algorithmicx(use algpseudocode as layout)学习记录,主要内容包括概述、Example、Details、行号、行、注释、引用、Commands、if block、使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
这几天写算法作业,提供的tex文件中使用algorithmicx
书写伪代码,虽然也会用algorithm2e
,但技多不压身,现在就来学一学。
概述
相比algorithm2e
的复杂命令(If
,eIf
,uIf
,...),algorithmicx
真是十分的简单。要使用algorithmicx
,可以使用algpseudocode
作为layout,它会导入algorithmicx
。[1]
关于algorithm
, algorithmic
, algorithmicx
, algorithm2e
, algpseudocode
之间的关系,可以看看stackexchange上的回答。
\usepackage{algorithm}
\usepackage[noend]{algpseudocode} %end or noend
Example
view code
\begin{algorithm}[H]
\caption{Quick Sort algorithm}\label{alg:sampling1}
\begin{algorithmic}[1]
\Function {QSort} {$l,r$}
\If{$l \geq r$}
\State \textbf{exit}
\EndIf
\State pick a random element in $A[l], ..., A[r]$ as $pivot$
\State $i=l, j=r$
\While{$i\leq j$}
\While{$A[i] < pivot$} $i=i+1$
\EndWhile
\While{$A[j] > pivot$} $j=j-1$
\EndWhile
\If{$i \leq j$}
\State \textsc{SWAP}($A[i], A[j]$)
\State $i=i+1$
\State $j=j-1$
\EndIf
\EndWhile
\State \textsc{QSort}($l, j$)
\State \textsc{QSort}($i, r$)
\EndFunction
\end{algorithmic}
\end{algorithm}
Details
行号
每个算法以\begin{algorithmic}[lines]
命令开头。lines
为0则不标行号;若为1则每行标记一次;若为n则每n行标记一次。
行
用\State
开启一个新行。
注释
用\Comment
命令来添加注释,该命令和algorithmic
不兼容。
引用
我们可以用\label
来给算法或者算法的某一行来设置标签,并在随后用\ref
来引用它。在用\ref
引用某一行时,只会显示行号,有时我们想精确地表示它是具体哪个算法的哪一行,可以使用\algref
,方法如下。
view code
\begin{algorithm}
\caption{Euclid’s algorithm}\label{euclid}
\begin{algorithmic}[1]
\Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
\State $r\gets a\bmod b$
\While{$r\not=0$}\Comment{We have the answer if r is 0}
\State $a\gets b$
\State $b\gets r$
\State $r\gets a\bmod b$
\EndWhile\label{euclidendwhile}
\State \textbf{return} $b$\Comment{The gcd is b}
\EndProcedure
\end{algorithmic}
\end{algorithm}
The \textbf{while} in algorithm \ref{euclid} ends in line \ref{euclidendwhile}, so \algref{euclid}{euclidendwhile} is the line we seek.
Commands
if block
\If
, \Else
, \ElseIf
, \EndIf
for block
\For
, \ForAll
, \EndFor
while block
\While
, \EndWhile
repeat block
\Repeat
, \Until
procedure block
\Procedure
, \EndProcedure
function block
\Function
, \EndFunction
原文地址:https://www.cnblogs.com/coco02/p/17250688.html
- 如何买卖股票?不要慌,我有妙招!
- 2017.11.7解题报告
- TensorFlow从0到1 - 11 - 74行Python实现手写体数字识别
- 让priority_queue支持小根堆的几种方法
- 一招解决4道leetcode hard题,动态规划在字符串匹配问题中的应用
- 细数Python中的数据类型以及他们的方法
- 洛谷 P3807 【模板】卢卡斯定理
- 数据城堡参赛代码实战篇(六)---使用sklearn进行数据标准化及参数寻优
- 震惊!Vector两行代码求逆序对,六行代码过普通平衡树
- 数据城堡参赛代码实战篇(五)---使用sklearn解决分类问题
- 洛谷P1894 [USACO4.2]完美的牛栏The Perfect Stall
- [编程经验]Python生成器、迭代器与yield语句小结
- TensorFlow从0到1 - 12 - TensorFlow构建3层NN玩转MNIST
- 数据城堡参赛代码实战篇(四)---使用pandas合并数据表
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Android自定义View实现五星好评效果
- Android通过ViewModel保存数据实现多页面的数据共享功能
- Android自定义View实现炫酷进度条
- OpenSSL的VC编程 - MD5
- 详解AndroidStudio中代码重构菜单Refactor功能
- Android自定义View仿QQ运动步数效果
- Kotlin类型安全构建器的一次运用记录
- 两个surfaceView实现切换效果
- AndroidStudio4.0 New Class的坑(小结)
- Android日历控件的实现方法
- GetDiskFreeSpaceEx函数的一点处理
- Flutter轮播图效果的实现步骤
- Android自定义View实现五子棋游戏
- android简单自定义View实现五子棋
- 文件操作API