使用algorithmicx包
\begin{algorithm} \caption{Sum of integers from 1 to n.} \begin{algorithmic}[1] \Procedure {Sum}{$n$} \State $sum \gets 0 \hfill \triangleright {\rm \ Initialization}$ \For{$i \gets 1,n$} \State $sum \gets sum + i$ \EndFor \State \Return{$sum$} $ \hfill \triangleright {\rm \ Return}$ \EndProcedure \end{algorithmic} \end{algorithm}
结果如下: