
for-loop를 사용하여 TeXstudio를 사용하여 알고리즘을 작성하는 데 도움이 필요합니다. 가우스 소거 방법에 대한 코드는 다음과 같습니다.
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}