1. 程式人生 > >學習虛擬碼

學習虛擬碼

\documentclass{article}
\usepackage{dsfont}
\usepackage{algorithm}
\usepackage{algorithmicx}
\usepackage{algpseudocode}
\usepackage{mathtools}
\newcommand{\minbox}[2]{%
  \mathmakebox[\ifdim#1<\width\width\else#1\fi]{#2}}
\newcommand{\Let}[2]{\State $ \minbox{1em}{#1} \gets #2 $}
\algnewcommand{\Local}{\State\textbf{local variables: }}
\begin{document}
\begin{algorithm}
  \caption{Mandelbrot set}
  \label{alg:mandelbrot}
  \begin{algorithmic}[1]
    \Require{$c_x, c_y, \Sigma_{\max} \in \mathds{R},
      \quad i \in \mathds{N}, \quad i_{\max} > 0,
      \quad \Sigma_{\max} > 0$}
    \Function{mandelbrot}{$c_x, c_y, i_{\max},
              \Sigma_{\max}$}
      \Local{$x, y, x^\prime, y^\prime, i, \Sigma$}
      \Let{x, y, i, \Sigma}{0}
        \Comment{initial zero value for all}
      \While{$\Sigma \leq \Sigma_{\max}$
             and $i < i_{\max}$}
        \Let{x^\prime}{x^2 - y^2 + c_x}
        \Let{y^\prime}{2xy + c_y}
        \Let{m}{x^\prime}
        \Let{y}{y^\prime}
        \Let{\Sigma}{x^2 + y^2}
      \EndWhile
      \If{$i < i_{\max}$}
        \State \Return{$i$}
      \EndIf
      \State\Return{0}
    \EndFunction
  \end{algorithmic}
\end{algorithm}