Files
AlgorithmNotes/Foundations/overview/add-binary.tex
pezy_mbp 8b472fe019 Finally finished 2.1.
1. added code for 2.1-3
2. added answer for 2.1-4
2015-05-12 01:21:56 +08:00

20 lines
463 B
TeX

\documentclass{article}
\usepackage{clrscode3e}
\begin{document}
\title{2.1-4}
\author{pezy}
\maketitle
\begin{codebox}
\Procname{$\proc{Add-Binary}(A, B)$}
\li $C \gets \proc{Array}(\attrib{A}{length}+1)$
\li $carry \gets 0$
\li \For $j = \attrib{A}{length}$ \Downto $1$
\li \Do
$C[j+1] \gets (A[j] + B[j] + carry) \% 2$
\li $carry \gets (A[j] + B[j] + carry) / 2$
\End
\li $C[1] \gets carry$
\li \Return $C$
\end{codebox}
\end{document}