LaTex算法模板

\documentclass[10.5pt]{article}

%宏包
\usepackage[UTF8]{ctex}
\usepackage[top=1cm, bottom=2cm, left=2cm, right=2cm]{geometry}
\usepackage{algorithm}  
\usepackage{algpseudocode}  
\usepackage{amsmath}  
\renewcommand{\algorithmicrequire}{\textbf{Input:}}  % Use Input in the format of Algorithm  
\renewcommand{\algorithmicensure}{\textbf{Output:}} % Use Output in the format of Algorithm
%正文
\begin{document}
	\begin{algorithm}  
		\caption{扩展欧几里得非递归算法}  
		\begin{algorithmic}[1] %每行显示行号  
			\Require 整数a,b  
			\Ensure 求解一组x,y,满足ax+by=gcd(a,b) 
			\Function {exgcd}{$a,b,x,y$}//   
			\State $x0 \gets 1,y0 \gets 0,x1 \gets 0,y1 \gets 1,x \gets 1,y \gets 0$
			\State $r \gets a$\%$b$
			\State $q \gets (a-r)//b$
		    \While{$r$}
		    	\State $x \gets x0-q*x1,y \gets y0-q*y1$
		        \State $x0 \gets x1,y0 \gets y1$
		       	\State $x1 \gets x,y1 \gets y$
		      	\State $a \gets b,b \gets r,r \gets a$\%$b$
		       	\State $q \gets (a-r)//b$
			\EndWhile
			\State \Return{$a,x,y$}  
			\EndFunction    
		\end{algorithmic}  
	\end{algorithm}
\end{document}


\documentclass[10.5pt]{article}

%宏包
\usepackage[UTF8]{ctex}
\usepackage[top=1cm, bottom=2cm, left=2cm, right=2cm]{geometry}
\usepackage{algorithm}  
\usepackage{algpseudocode}  
\usepackage{amsmath}  
\renewcommand{\algorithmicrequire}{\textbf{Input:}}  % Use Input in the format of Algorithm  
\renewcommand{\algorithmicensure}{\textbf{Output:}} % Use Output in the format of Algorithm
%正文
\begin{document}
	\begin{algorithm}  
		\caption{费马素性检测算法}  
		\begin{algorithmic}[1] %每行显示行号  
			\Require 整数n,随机次数times  
			\Ensure n是否是素数 
			\Function {$Is\_Prime$}{$n,times$}//Quick\_Mod为快速模幂算法
			\For{$i = 0 \to times$}
			\If {$Quick_Mod(1+random(n),n-1,n) \neq 1$} 
			\State break 
			\EndIf
			\EndFor  
		    \If {$i == 5$}
		    \State \Return{$True$}
		    \Else 
		    \State \Return{$False$}
		    \EndIf
		      
			\EndFunction    
		\end{algorithmic}  
	\end{algorithm}
\end{document}

 

\documentclass[10.5pt]{article}

%宏包
\usepackage[UTF8]{ctex}
\usepackage[top=1cm, bottom=2cm, left=2cm, right=2cm]{geometry}
\usepackage{algorithm}  
\usepackage{algpseudocode}  
\usepackage{amsmath}  
\renewcommand{\algorithmicrequire}{\textbf{Input:}}  % Use Input in the format of Algorithm  
\renewcommand{\algorithmicensure}{\textbf{Output:}} % Use Output in the format of Algorithm
%正文
\begin{document}
	 \begin{algorithm} 
	 	\caption{分支定界法数据定义} 
	 	 \begin{algorithmic}[1] %每行显示行号
	 	 	\State \#define MAX\_COST 1500
	 	 	\State \#define MAX 110
	 	 	\State \#define MAX\_LEN 9999
	 	 	\State const int inf = 0x3f3f3f3f;
	 	 	\State int n;                 //节点数
	 	 	
	 	 	\State int visit[MAX];        //记录节点i是否访问过
	 	 	\State int currentPath[MAX];      //记录当前路径
	 	 	\State int shortestPath[MAX];     //记录最短路径
	 	 	\State int cnt;              //最短路径上的节点下标
	 	 	
	 	 	\State int currentDistance;           //记录当前路径的长度
	 	 	\State int shortestDistance;         //记录已知最短路径的长度
	 	 	
	 	 	\State int currentCost;           //记录当前路径的费用
	 	 	\State int cheappestCost;         //记录当前最短路径的费用
	 	 	
	 	 	\State int shortestDistances[MAX];  //记录从源点到节点i的最短路径长度
	 	 	\State int cheappestCosts[MAX];      //记录从源点到节点i最短路径对应的费用
	 	 	
	 	 	\State int citydist[MAX][MAX];  //城市间的距离矩阵
	 	 	\State int citycost[MAX][MAX];  //城市间彼此到达的花费
	 	 \end{algorithmic}
	 \end{algorithm}
 \begin{algorithm}  
	\caption{分支定界法求最优路线}  
	\begin{algorithmic}[1] %每行显示行号  
		\Require $50\times50$有向图邻接矩阵(边权表示路径长度),$50\times50$费用矩阵(对应路径的费用)  
		\Ensure 花费限制1500下,从甲城市[1]到乙城市[50]的最短路径  
		\Function {BranchAndBound}{$depth, v$}//分支定界法,deep表示当前搜索深度(最优路径节点数),v代表当前访问的节点    
		\State add v to currentPath //当前节点添加到路径
		\If {$depth = 0$} //第一个添加进来的节点
		\State $visit[v] \gets true$   //标记已访问 
		\EndIf
		\If {$depth >= 50$}  \Return //回溯(1) \EndIf 
		\If {$v = 50$} //找到终点,回溯(2)
		\State add v to currentPath
		\State $shortestPath \gets currentPath$ //当前最优路径赋值给全局最优路径
		\State $shortestDistances[50] \gets currentDistance$ //当前最短路径长度赋值给全局最短,更新界
		\State $shortestDistance \gets currentDistance$
		\State $cheappestCosts[50] \gets currentCost$ //当前最优路径下的费用赋值给全局,更新界
		\State $cheappestCost \gets currentCost$
		\State \Return
		\EndIf
		\For{$i = 1 \to 50$}  
		\If {$citydist[v][i] < MAX\_LEN$}  //当前节点V的邻接点
		\State $currentDistance \gets currentDistance+citydist[v][i]$ //为当前路径添加一条到邻接点的路径
		\State $currentCost \gets currentCost+citycost[v][i]$ //同时添加花费值
		\If {$visit[i] = true$} continue //回溯(6)
		\EndIf
		\If {$currentDistance<=shortestDistances[i]\&\¤tCost<=MAX\_COST$} //剪枝(3)(5)满足if条件,即是在二叉搜索树中选择走左分支,将i节点加入当前路径
		\If {$currentCost<=cheappestCosts[i]$} //剪枝(7),更新界
		\State $shortestDistances[i] \gets currentDistance$ //更新界
		\State $cheappestCosts[i] \gets currentCost$
		\EndIf
		
		\If {$currentDistance<=min(shortestDistances[i],shortestDistances[n])$}//剪枝(4)
		\State $visit[i] \gets true$ //标记已访问
		\State \Call{BranchAndBound}{$depth+1, i$} //向深层搜索
		\State $visit[i] \gets false$ //恢复标记,相当于在二叉搜索树中回溯到当前待分支节点,选择走该节点的右分支,即不将i节点加入路径,改走其他邻接点
		\EndIf
		\EndIf
		\State $currentDistance \gets currentDistance-citydist[v][i]$
		\State $currentCost \gets currentCost-citycost[v][i]$//将当前最短路径长度和对应花费恢复到深搜前的状态
		\EndIf
		\EndFor 
		\EndFunction    
	\end{algorithmic}  
\end{algorithm}
\end{document}
\documentclass[10.5pt]{article}

%宏包
\usepackage[UTF8]{ctex}
\usepackage[top=1cm, bottom=2cm, left=2cm, right=2cm]{geometry}
\usepackage{algorithm}  
\usepackage{algpseudocode}  
\usepackage{amsmath}  
\renewcommand{\algorithmicrequire}{\textbf{Input:}}  % Use Input in the format of Algorithm  
\renewcommand{\algorithmicensure}{\textbf{Output:}} % Use Output in the format of Algorithm
%正文
\begin{document}
	\begin{algorithm}  
		\caption{Solovay\_Strassen素性检测算法}  
		\begin{algorithmic}[1] %每行显示行号  
			\Require 整数n,随机次数times  
			\Ensure n是否是素数 
			\Function {$Solovay\_Strassen$}{$n,times$}//Quick\_Mod为快速模幂算法,random产生随机数,Jacobi计算雅可比符号
			\For{$i = 0 \to times$}
			\While{$Rand\_Num > 1 \&\& Rand\_Num <= n-1$}
			\State $Rand\_Num \gets random(n)$
			\EndWhile
			\State $r \gets Quick\_Mod(Rand\_Num,(n-1)/2,n)$
			
			\If {$not (r==1 || r==n-1)$} 
			\State \Return{$0$}
			\EndIf
			\State $jac \gets Jacobi(Rand\_Num,n) //Jacobi计算雅可比符号$
			\If {$jac < 0$} 
			\State $jac \gets jac+n$
			\EndIf
			\If {$jac \neq r$} 
			\State \Return{$0$}
			\EndIf
			\State \Return{$1$}
			\EndFor  
		      
			\EndFunction    
		\end{algorithmic}  
	\end{algorithm}
\end{document}

LaTex算法模板_第1张图片

 

你可能感兴趣的:(北航,模板)