[Tex/LaTex] How to make caption above in algorithm

algorithmscaptions

How can I make the caption "Algorithm 1" be placed above the first line and the text still inside the two first lines?
And how can the line styles be changed in the algorithm environment?

\begin{algorithm}
\caption{Calculate $y = x^n$}
\label{alg1}
\begin{algorithmic}
\REQUIRE $n \geq 0 \vee x \neq 0$
\ENSURE $y = x^n$
\STATE $y \leftarrow 1$
\IF{$n < 0$}
\STATE $X \leftarrow 1 / x$
\STATE $N \leftarrow -n$
\ELSE
\STATE $X \leftarrow x$
\STATE $N \leftarrow n$
\ENDIF
\WHILE{$N \neq 0$}
\IF{$N$ is even}
\STATE $X \leftarrow X \times X$
\STATE $N \leftarrow N / 2$
\ELSE[$N$ is odd]
\STATE $y \leftarrow y \times X$
\STATE $N \leftarrow N - 1$
\ENDIF
\ENDWHILE
\end{algorithmic}
\end{algorithm}

enter image description here

Best Answer

You have to define a new float style:

\documentclass{article}
\usepackage{algorithm,algorithmic}

\makeatletter
\newcommand\floatc@ruled@gabriel[2]{#2\par}
\newcommand\fs@ruledgabriel{%
  \def\@fs@cfont{\bfseries}\let\@fs@capt\floatc@ruled@gabriel
  \def\@fs@pre{%
    {\@fs@cfont\strut\fname@algorithm\ \thealgorithm}\par
    \hrule height.8pt depth0pt \kern2pt
  }%
  \def\@fs@post{\kern2pt\hrule\relax}%
  \def\@fs@mid{\kern2pt\hrule\kern2pt}%
  \let\@fs@iftopcapt\iftrue}
\makeatletter
\floatstyle{ruledgabriel}
\restylefloat{algorithm}


\begin{document}

\begin{algorithm}
\caption{Calculate $y = x^n$}
\label{alg1}
\begin{algorithmic}
\REQUIRE $n \geq 0 \vee x \neq 0$
\ENSURE $y = x^n$
\STATE $y \leftarrow 1$
\IF{$n < 0$}
\STATE $X \leftarrow 1 / x$
\STATE $N \leftarrow -n$
\ELSE
\STATE $X \leftarrow x$
\STATE $N \leftarrow n$
\ENDIF
\WHILE{$N \neq 0$}
\IF{$N$ is even}
\STATE $X \leftarrow X \times X$
\STATE $N \leftarrow N / 2$
\ELSE[$N$ is odd]
\STATE $y \leftarrow y \times X$
\STATE $N \leftarrow N - 1$
\ENDIF
\ENDWHILE
\end{algorithmic}
\end{algorithm}

\end{document}

enter image description here