![if else 循环并重复直到循环不适用于算法包](https://linux22.com/image/466566/if%20else%20%E5%BE%AA%E7%8E%AF%E5%B9%B6%E9%87%8D%E5%A4%8D%E7%9B%B4%E5%88%B0%E5%BE%AA%E7%8E%AF%E4%B8%8D%E9%80%82%E7%94%A8%E4%BA%8E%E7%AE%97%E6%B3%95%E5%8C%85.png)
代码:
\begin{algorithm}[!t]
\begin{algorithmic}[1]
\caption{Adaptive Random Search}\label{algo_ARS}
\State define the probability of local search as $P_s$ and global search as 1 - $P_s$
\Repeat
\State generate a random number r(0,1)
\If{r \leq $P_s$}
\State local search is selected
\Else
\State global search is selected
\Until{$X_{ARS}$ is accepted}
\end{algorithmic}
\end{algorithm}
我编写的伪代码不起作用。我无法获得直到部分作为输出。
答案1
我不知道您具体使用了哪些包,但您可能缺少一个\EndIf
关闭嵌套 if 语句的命令:
\documentclass{article}
\usepackage{algorithm, algpseudocode}
\begin{document}
\begin{algorithm}[!t]
\begin{algorithmic}[1]
\caption{Adaptive Random Search}\label{algo_ARS}
\State define the probability of local search as $P_s$ and global search as $1 - P_s$
\Repeat
\State generate a random number $r(0,1)$
\If{$r \leq P_s$}
\State local search is selected
\Else
\State global search is selected
\EndIf
\Until{$X_\mathrm{ARS}$ is accepted}
\end{algorithmic}
\end{algorithm}
\end{document}