用texstudio写伪代码

示例:

\documentclass[11pt,twoside,a4paper]{article}

\usepackage{algorithm}
\usepackage{algorithmicx}
\usepackage{algpseudocode}
\usepackage{amsmath}
\usepackage[top=2cm, bottom=2cm, left=2cm, right=2cm]{geometry}
\begin{document}

%% 写算法伪代码或者流程的前期准备
\renewcommand{\algorithmicrequire}{\textbf{Input:}}  % Use Input in the format of Algorithm
\renewcommand{\algorithmicensure}{\textbf{Output:}} % Use Output in the format of Algorithm

\begin{algorithm}[h]
  \caption{Pseudocode of Simulated Annealing Algorithm} % 名称
  \begin{algorithmic}[1]
    \Require
      $x_0$: initial individual or state;
      $T_0$: a high enough initial temperature;
      $T_{min}$: the lowest limit of temperature;
    \Ensure
       optimal state or approximate optimal state;
       \State set $x_0 = x_{best}$, compute initial energy function $E(x_0)$;
       \While {$T > T_{min}$}
         \For{$i = 1$; $i<n$; $i++$ }
      \State perturb current state $x_i$ for a new state $x_{new}$ and compute energy function $E(x_{new})$;
      \State compute $\Delta$ = $E(x_{new}-E(x_{(i)})$;
      \If {$\Delta$$E<0$} \State $x_{best} = x_{new}$
      \Else \State the probability $P = exp(-dE/T_{(i)})$;
      \If {$rand(0,1) < P$ }\State $x_{best} = x_{new}$
      \Else \State $x_{best} = x_{best}$
      \EndIf
     \EndIf
     \EndFor
      \State $T = T * $ $ \alpha$, where $\alpha$ is decay factor  ;
    \EndWhile
  \end{algorithmic}
\end{algorithm}

\end{document}

效果:

伪代码生成的效果展示

代码分析:

  1. 指定文档类型
\documentclass[11pt,twoside,a4paper]{article}  //
  1. 加载宏包:
\usepackage{algorithm}
\usepackage{algorithmicx}
\usepackage{algpseudocode}
\usepackage{amsmath}
\usepackage[top=2cm, bottom=2cm, left=2cm, right=2cm]{geometry}
  1. 文档开始结束标志
\begin{document}
		在begin和end中间插入代码
\end{document}
  1. 写算法伪代码之前的准备
\renewcommand{\algorithmicrequire}{\textbf{Input:}}  % Use Input in the format of Algorithm
\renewcommand{\algorithmicensure}{\textbf{Output:}} % Use Output in the format of Algorithm
  1. 算法开始和结束标志:
\begin{algorithm}[h]
	在中间插入算法的代码。
\end{algorithm}
  • 7
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值