
TeXstudio で for ループを使用してアルゴリズムを書くのにご協力をお願いします。ガウス消去法のコードは次のとおりです。
for (int i = 0; i < N-1; i++) {
for (int j = i; j < N; j++) {
double ratio = A[j][i]/A[i][i];
for (int k = i; k < N; k++) {
A[j][k] -= (ratio*A[i][k]);
b[j] -= (ratio*b[i]);
}
}
}
答え1
コードを書く一つの方法は、パッケージを使うことですlistings
。ガウス消去法のアルゴリズムは次のように記述されます。
\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\lstset{ %
backgroundcolor = \color{white}, % Background color
keywordstyle = \color{blue}, % Keyword style (just color here)
numbers = left, % Add line number to the left of the code
captionpos = b % Caption at the bottom of the listing
}
\begin{document}
\begin{lstlisting}[language = C, caption={Gaussian elimination algorithm}]
for (int i = 0; i < N-1; i++) {
for (int j = i; j < N; j++) {
double ratio = A[j][i]/A[i][i];
for (int k = i; k < N; k++) {
A[j][k] -= (ratio*A[i][k]);
b[j] -= (ratio*b[i]);
}
}
}
\end{lstlisting}
\end{document}
コードにキャプションを付けたくない場合は、環境caption
の先頭にあるオプションを削除するだけですlstlisting
。言語が C でない場合は、language
使用している言語にいつでもオプションを変更できます。
答え2
対応する疑似コード。ここでは、algorithm
およびalgpseudocode
パッケージが使用されます。
\documentclass{article}
\usepackage{algorithm} % Required for pseudo code
\usepackage[noend]{algpseudocode} % Required for pseudo code
\renewcommand{\algorithmicrequire}{\textbf{Input:}} % Changes 'Require' to 'Input'
\begin{document}
\begin{algorithm}
\caption{Caption of pseudo code}
\begin{algorithmic}
\Require $A$
\For{$i \gets 0$ to $N - 2 $}
\For{$j \gets i$ to $N - 1 $}
\State $ratio \gets A[j][i]/A[i][i]$
\For{$k \gets i$ to $N - 1 $}
\State $A[j][k] \gets A[j][k] - (ratio*A[i][k])$
\State $b[j] \gets b[j] - (ratio*b[i])$
\EndFor
\EndFor
\EndFor
\end{algorithmic}
\label{alg:code_label}
\end{algorithm}
\end{document}