Latex写算法的伪代码排版
http://blog.csdn.net/lwb102063/article/details/53046265
algorithmicx例子
相应代码:
- \documentclass[11pt]{ctexart}
- \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{algorithm}
- \caption{用归并排序求逆序数}
- \begin{algorithmic}[1] %每行显示行号
- \Require 数组,数组大小
- \Ensure 逆序数
- \Function {MergerSort}{}
- \State
- \If {}
- \State
- \State \Call{MergerSort}{}
- \State \Call{MergerSort}{}
- \State \Call{Merger}{}
- \EndIf
- \State \Return{}
- \EndFunction
- \State
- \Function{Merger}{}
- \State
- \State
- \State
- \State
- \While{ \textbf{and} }
- \If{}
- \State
- \Else
- \State
- \State
- \EndIf
- \EndWhile
- \While{}
- \State
- \EndWhile
- \While{}
- \State
- \EndWhile
- \For{}
- \State
- \EndFor
- \State \Return{}
- \EndFunction
- \end{algorithmic}
- \end{algorithm}
- \end{document}
algorithm例子
前期准备
代码:
- \begin{algorithm}[htb]
- \caption{ Framework of ensemble learning for our system.}
- \label{alg:Framwork}
- \begin{algorithmic}[1]
- \Require
- The set of positive samples for current batch, ;
- The set of unlabelled samples for current batch, ;
- Ensemble of classifiers on former batches, ;
- \Ensure
- Ensemble of classifiers on the current batch, ;
- \State Extracting the set of reliable negative and/or positive samples from with help of ;
- \label{code:fram:extract}
- \State Training ensemble of classifiers on , with help of data in former batches;
- \label{code:fram:trainbase}
- \State ;
- \label{code:fram:add}
- \State Classifying samples in by ;
- \label{code:fram:classify}
- \State Deleting some weak classifiers in so as to keep the capacity of ;
- \label{code:fram:select} \\
- \Return ;
- \end{algorithmic}
- \end{algorithm}
example 2
代码:
- \begin{algorithm}[h]
- \caption{An example for format For \& While Loop in Algorithm}
- \begin{algorithmic}[1]
- \For{each }
- \State initialize a tree with only a leaf (the root);
- \State
- \EndFor
- \ForAll { such that }
- \label{code:TrainBase:getc}
- \State ;
- \label{code:TrainBase:pos}
- \EndFor;
- \For{; ; }
- \State Your source here;
- \EndFor
- \For{ to }
- \State Your source here;
- \EndFor
- \State Reusing recent base classifiers.
- \label{code:recentStart}
- \While {}
- \State Selecting the most recent classifier from ;
- \State ;
- \State ;
- \EndWhile
- \label{code:recentEnd}
- \end{algorithmic}
- \end{algorithm}
example 3
代码:
- \begin{algorithm}[h]
- \caption{Conjugate Gradient Algorithm with Dynamic Step-Size Control}
- \label{alg::conjugateGradient}
- \begin{algorithmic}[1]
- \Require
- : objective funtion;
- : initial solution;
- : step size;
- \Ensure
- optimal
- \State initial and ;
- \Repeat
- \State compute gradient directions ;
- \State compute Polak-Ribiere parameter ;
- \State compute the conjugate directions ;
- \State compute the step size ;
- \Until{()}
- \end{algorithmic}
- \end{algorithm}
example 4
代码:
- \makeatletter
- \def\BState{\State\hskip-\ALG@thistlm}
- \makeatother
- \begin{algorithm}
- \caption{My algorithm}\label{euclid}
- \begin{algorithmic}[1]
- \Procedure{MyProcedure}{}
- \State
- \State
- \BState \emph{top}:
- \If {} \Return false
- \EndIf
- \State
- \BState \emph{loop}:
- \If {}
- \State .
- \State .
- \State \textbf{goto} \emph{loop}.
- \State \textbf{close};
- \EndIf
- \State .
- \State \textbf{goto} \emph{top}.
- \EndProcedure
- \end{algorithmic}
- \end{algorithm}
algorithm2e例子
algorithm2e包可能会与其它包产生冲突,一个常见的错误提示是“Too many }'...”。为了解决这个问题,要在引入algorithm2e包之前加入下面的命令:
- \makeatletter
- \newif\if@restonecol
- \makeatother
- \let\algorithm\relax
- \let\endalgorithm\relax
所以前期准备:
- \makeatletter
- \newif\if@restonecol
- \makeatother
- \let\algorithm\relax
- \let\endalgorithm\relax
- \usepackage[linesnumbered,ruled,vlined]{algorithm2e}%[ruled,vlined]{
- \usepackage{algpseudocode}
- \usepackage{amsmath}
- \renewcommand{\algorithmicrequire}{\textbf{Input:}} % Use Input in the format of Algorithm
- \renewcommand{\algorithmicensure}{\textbf{Output:}} % Use Output in the format of Algorithm
example 1
代码:
- \begin{algorithm}
- \caption{identify Row Context}
- \KwIn{, = and similarity threshold }
- \KwOut{}
- \;
- \For{}
- {
- float \;
- \;
- \While{not end of }
- {
- compute Jaro()()\;
- \If{}
- {
- replace with \;
- }
- }
- \;
- }
- return \;
- \end{algorithm}
example 2
代码:
- \begin{algorithm}
- \caption{Service checkpoint image storage node and routing path selection}
- \LinesNumbered
- \KwIn{host server that is fetched from, that belongs to, that belongs to}
- \KwOut{Service image storage server ,and the image transfer path }
- = Storage node selection(, ,,)\;
- \If{ null}
- {
- select a path from to and assign the path to \;
- }
- \textbf{final} \;
- \textbf{return} and ;
- \end{algorithm}
example 3
代码:
- \begin{algorithm}
- \caption{Storage node selection}
- \LinesNumbered
- \KwIn{host server that the checkpoint image is fetched from, that belongs to, that belongs to}
- \KwOut{Image storage server }
- \For{ each host server in the same subnet with }
- {
- \If{ is not a service providing node or checkpoint image storage node of }
- {
- add to \;
- }
- }
- sort by reliability desc\;
- init ;
- \For{ each in }
- {
- \If{ of and size of }
- {
- assign to \;
- goto final\;
- }
- }
- clear \;
- add all other subnets in to \;
- \For{ each subnet in }
- {
- clear \;
- \For {each in }
- {
- \If{ is not a service providing node or checkpoint image storage node of }
- {
- add to \;
- }
- }
- sort all host in by reliability desc\;
- \For{ each in }
- {
- \If{ of and size of }
- {
- assign to \;
- goto final\;
- }
- }
- }
- \textbf{final} \;
- \textbf{return} ;
- \end{algorithm}
代码:
- \begin{algorithm}
- \caption{Delta checkpoint image storage node and routing path selection}
- \LinesNumbered
- \KwIn{host server that generates the delta checkpoint image , that belongs to, that belongs to}
- \KwOut{Delta image storage server ,and the image transfer path }
- = Storage node selection(, ,,)\;
- \If{ null}
- {
- the delta checkpoint image is stored in the central storage server\;
- goto final\;
- }
- construct weighted topological graph of \;
- calculate the shortest path from to in by using the Dijkstra algorithm\;
- \textbf{final} \;
- \textbf{return} and ;
- \end{algorithm}
example 5
- \documentclass[8pt,twocolumn]{ctexart}
- \usepackage{amssymb}
- \usepackage{bm}
- \usepackage{textcomp} %命令\textacutedbl的包,二阶导符号
- % Page length commands go here in the preamble
- \setlength{\oddsidemargin}{-0.25in} % Left margin of 1 in + 0 in = 1 in
- \setlength{\textwidth}{9in} % 纸张宽度Right margin of 8.5 in - 1 in - 6.5 in = 1 in
- \setlength{\topmargin}{-.75in} % Top margin of 2 in -0.75 in = 1 in
- \setlength{\textheight}{9.2in} % Lower margin of 11 in - 9 in - 1 in = 1 in
- \setlength{\parindent}{0in}
- \makeatletter
- \newif\if@restonecol
- \makeatother
- \let\algorithm\relax
- \let\endalgorithm\relax
- \usepackage[linesnumbered,ruled,vlined]{algorithm2e}%[ruled,vlined]{
- \usepackage{algpseudocode}
- \renewcommand{\algorithmicrequire}{\textbf{Input:}}
- \renewcommand{\algorithmicensure}{\textbf{Output:}}
- \begin{document}
- \begin{algorithm}
- \caption{component matrices computing}
- \LinesNumbered
- \KwIn{}
- \KwOut{ for to }
- \textbf{Initialize} all //which can be seen as the round iterations\;
- {\hspace*{-1pt}\textacutedbl} //if we need to judge whether is true then {\hspace*{-1pt}\textacutedbl} denotes \;
- \For{ each }
- {// round iterations\;
- \;
- //if the rollback shown as is needed, denotes \;
- \;
- }
- \Repeat(//other rounds of iterations for computing component matrices){ or maximum iterations exhausted}
- {
- //if we need to judge whether is true then denotes \;
- \For{ each }
- {
- \If{}
- {
- \;
- \;
- \;
- \;
- }
- \ElseIf{}
- {
- \If{\hspace*{-1pt}\textacutedbl}
- {
- \;
- // if is true then rollback as \;
- \;
- }
- \Else
- {
- \;
- \;
- \;
- \;
- }
- }
- \Else
- {
- \;
- \;
- \;
- }
- }
- \hspace*{-1pt}\textacutedbl\;
- }
- \end{algorithm}
- \end{document}
- \usepackage[ruled,linesnumbered]{algorithm2e}
- \usepackage{amsmath}
- \begin{algorithm}
- \caption{Learning algorithm of R2P}
- \label{alg:r2p}
- \KwIn{ratings , joint demographic representations ,learning rate ,maximum iterative number , negative sampling number \;}
- \KwOut{interaction matrix , movie vectors \;}
- Initialize randomly\;
- \;
- For convenience, define \; %\varphi_n\bm{W}\vec{y}_n
- \While{not converged \rm{or} }
- {
- t = t+1\;
- \For{}
- {
- \;\label{algline:W}
- \For{}
- {
- \;\label{algline:V}
- }
- \For{}
- {
- sample negative sample from \;
- \;
- \For{}
- {
- \;
- }
- }
- }
- \;
- }
- return \;
- %\end{algorithmic}
- \end{algorithm}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通