Mac:TexStudio 插入伪代码

时间:2022-04-29 06:14:26
\documentclass{article}

\usepackage{xeCJK}
\usepackage{fontspec}
\setmainfont{Times New Roman}

\usepackage{algorithm}
\usepackage{algpseudocode}

\usepackage{amsmath}
\usepackage{amssymb}

\begin{document}
\begin{algorithm}
    $input$:$n$个元素的升序数组$A[1...n]$和元素$x$ \\
    $output$:$x=A[j]$,$1\leqslant j\leqslant n$,则输出$j$,否则输出0
    \begin{algorithmic}[1]
        \Procedure{BSearch}{}
        \State{$low \gets 1 ; high \gets n ; j \gets 0$}
        \While{$low \leqslant high$ and $j=0$}
        \State{$mid \gets random(low,high)$}
        \If{$x=A[mid]$}
        \State{$j \gets mid$}
        \ElsIf{$x < A[mid]$}
        \State{$high \gets (mid-1)$}
        \Else
        \State{$low \gets (mid+1)$}
        \EndIf
        \EndWhile
        \State\Return{$j$}
        \EndProcedure
    \end{algorithmic}
\end{algorithm}

\end{document}