$作者:孟晨
链接:https://www.zhihu.com/question/26535085/answer/33135927
来源:知乎
著作权归作者所有,转载请联系作者获得授权。

\documentclass{article}
\usepackage{algorithm}
\usepackage{algorithmic}
\begin{document}
\begin{algorithm}
\caption{Calculate \(y = x^n\)}
\label{alg1}
\begin{algorithmic}
\REQUIRE \(n \geq 0 \vee x \neq 0\)
\ENSURE \(y = x^n\)
\STATE \(y \gets 1\)
\IF{\(n < 0\)}
\STATE \(X \gets 1 / x\)
\STATE \(N \gets -n\)
\ELSE
\STATE \(X \gets x\)
\STATE \(N \gets n\)
\ENDIF
\WHILE{\(N \neq 0\)}
\IF{\(N\) is even}
\STATE \(X \gets X \times X\)
\STATE \(N \gets N / 2\)
\ELSE[\(N\) is odd]
\STATE \(y \gets y \times X\)
\STATE \(N \gets N - 1\)
\ENDIF
\ENDWHILE
\end{algorithmic}
\end{algorithm}
\end{document}$

作者:孟晨
链接:https://www.zhihu.com/question/26535085/answer/33135927
来源:知乎
著作权归作者所有,转载请联系作者获得授权。

posted on 2016-12-04 10:32  yanhh  阅读(382)  评论(0编辑  收藏  举报