\documentclass{article}
\usepackage{algorithm}
\usepackage{algorithmic}
\begin{document}
\begin{minipage}{0.46\textwidth}
\begin{algorithm}[H]
\caption{foo}
\begin{algorithmic}[1]
\STATE let $S$ represent ..
\STATE let $P$ represent ..
\STATE let $N$ represent ..
\FORALL {$p \gets Rand(0,P) $}
\FORALL{$i \gets Rand(0,n)$}
\STATE Select $j \gets Rand(0,n)$ \\
$S[p,j], S[p,i] \gets S[p,i], S[p,j]$
\ENDFOR
\ENDFOR
\end{algorithmic}
\end{algorithm}
\end{minipage}
\end{document}
答案1
algorithm
该答案假设您使用和包的组合algorithmic
(问题中未提供此信息)。
的语法\FORALL
是循环变量的定义在括号中,但循环主体不在括号中。循环主体被认为是直到相应\ENDFOR
语句的所有内容。
梅威瑟:
\documentclass{article}
\usepackage{algorithm}
\usepackage{algorithmic}
\begin{document}
\begin{minipage}{0.66\textwidth}
\begin{algorithm}[H]
\caption{foo}
\begin{algorithmic}[1]
\STATE let $S$ represent ..
\STATE let $P$ represent ..
\STATE let $N$ represent ..
\FORALL{$p \gets Rand(0,P) $}
\FORALL{$i \gets Rand(0,n)$}
\STATE Select $j \gets Rand(0,n)$ \\
$S[p,j], S[p,i] \gets S[p,i], S[p,j]$
\ENDFOR
\ENDFOR
\end{algorithmic}
\end{algorithm}
\end{minipage}
\end{document}
结果: