page37.html
来自「wqeqwvrw rkjqhwrjwq jkhrjqwhrwq jkhrwq」· HTML 代码 · 共 81 行
HTML
81 行
<HTML>
<HEAD>
<TITLE>A Simple Example-Arithmetic Series Summation</TITLE>
</HEAD>
<BODY bgcolor="#FFFFFF">
<img src="cover75.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cover75.gif" alt="Logo" align=right>
<b>Data Structures and Algorithms
with Object-Oriented Design Patterns in C++</b><br>
<A NAME="tex2html2352" HREF="page38.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page38.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="next_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/next_motif.gif"></A> <A NAME="tex2html2350" HREF="page35.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page35.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="up_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/up_motif.gif"></A> <A NAME="tex2html2344" HREF="page36.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page36.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/previous_motif.gif"></A> <A NAME="tex2html2354" HREF="page9.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page9.html"><IMG WIDTH=65 HEIGHT=24 ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/contents_motif.gif"></A> <A NAME="tex2html2355" HREF="page620.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page620.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="index_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/index_motif.gif"></A> <BR><HR>
<H2><A NAME="SECTION003120000000000000000">A Simple Example-Arithmetic Series Summation</A></H2>
<P>
In this section we apply Axioms <A HREF="page36.html#axiomi" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page36.html#axiomi"><IMG ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A>, <A HREF="page36.html#axiomii" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page36.html#axiomii"><IMG ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A> and <A HREF="page36.html#axiomiii" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page36.html#axiomiii"><IMG ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A>
to the analysis of the running time of a program to compute the following
simple arithmetic series summation
<P> <IMG WIDTH=266 HEIGHT=43 ALIGN=BOTTOM ALT="displaymath58211" SRC="img20.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img20.gif" ><P>
The algorithm to compute this summation is given in Program <A HREF="page37.html#progsumc" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page37.html#progsumc"><IMG ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A>.
<P>
<P><A NAME="616"> </A><A NAME="progsumc"> </A> <IMG WIDTH=575 HEIGHT=143 ALIGN=BOTTOM ALT="program330" SRC="img22.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img22.gif" ><BR>
<STRONG>Program:</STRONG> Program to compute <IMG WIDTH=46 HEIGHT=27 ALIGN=MIDDLE ALT="tex2html_wrap_inline58213" SRC="img21.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img21.gif" ><BR>
<P>
<P>
The executable statements in Program <A HREF="page37.html#progsumc" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page37.html#progsumc"><IMG ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A>
comprise lines 3-6.
Table <A HREF="page37.html#tblsumc" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page37.html#tblsumc"><IMG ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A> gives the running times of each of these statements.
<P>
<P><A NAME="618"> </A>
<P>
<A NAME="tblsumc"> </A>
<DIV ALIGN=CENTER><P ALIGN=CENTER><TABLE COLS=3 BORDER FRAME=HSIDES RULES=GROUPS>
<COL ALIGN=CENTER><COL ALIGN=CENTER><COL ALIGN=LEFT>
<TBODY>
<TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP>
statement </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> time </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP COLSPAN=1> code</TD></TR>
</TBODY><TBODY>
<TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP>3 </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> <IMG WIDTH=88 HEIGHT=20 ALIGN=MIDDLE ALT="tex2html_wrap_inline58181" SRC="img9.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img9.gif" > </TD><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP> <tt>result = 0</tt></TD></TR>
<TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP>
4a </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> <IMG WIDTH=88 HEIGHT=20 ALIGN=MIDDLE ALT="tex2html_wrap_inline58181" SRC="img9.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img9.gif" > </TD><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP> <tt>i = 1</tt> </TD></TR>
<TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP>
4b </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> <IMG WIDTH=159 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline58219" SRC="img23.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img23.gif" > </TD><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP> <tt>i <= n</tt> </TD></TR>
<TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP>
4c </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> <IMG WIDTH=174 HEIGHT=26 ALIGN=MIDDLE ALT="tex2html_wrap_inline58221" SRC="img24.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img24.gif" > </TD><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP> <tt>++i</tt> </TD></TR>
<TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP>
5 </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> <IMG WIDTH=174 HEIGHT=26 ALIGN=MIDDLE ALT="tex2html_wrap_inline58221" SRC="img24.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img24.gif" > </TD><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP> <tt>result += i</tt> </TD></TR>
<TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP>
6 </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> <IMG WIDTH=96 HEIGHT=20 ALIGN=MIDDLE ALT="tex2html_wrap_inline58225" SRC="img25.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img25.gif" > </TD><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP> <tt>return result</tt> </TD></TR>
</TBODY><TBODY>
<TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP>TOTAL </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> <IMG WIDTH=226 HEIGHT=26 ALIGN=MIDDLE ALT="tex2html_wrap_inline58227" SRC="img26.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img26.gif" > </TD><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP> </TD></TR>
<TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP>
</TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> <IMG WIDTH=231 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline58229" SRC="img27.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img27.gif" > </TD><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP> </TD></TR>
</TBODY>
<CAPTION ALIGN=BOTTOM><STRONG>Table:</STRONG> Computing the running time of Program <A HREF="page37.html#progsumc" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page37.html#progsumc"><IMG ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A></CAPTION></TABLE>
</P></DIV><P>
<P>
Note that the <tt>for</tt> statement on line 4 of Program <A HREF="page37.html#progsumc" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page37.html#progsumc"><IMG ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A>
has been split across three lines in Table <A HREF="page37.html#tblsumc" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page37.html#tblsumc"><IMG ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A>.
This is because we analyze the running time of each of the elements
of a <tt>for</tt> statement separately.
The first element, the <em>initialization code</em>,
is executed once before the first iteration of the loop.
The second element, the <em>loop termination test</em>,
is executed before each iteration of the loop begins.
Altogether, the number of times the termination test is executed
is one more than the number of times the loop body is executed.
Finally, the third element, the <em>loop counter increment step</em>,
is executed once per loop iteration.
<P>
Summing the entries in Table <A HREF="page37.html#tblsumc" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page37.html#tblsumc"><IMG ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A>
we get that the running time, <I>T</I>(<I>n</I>),
of Program <A HREF="page37.html#progsumc" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page37.html#progsumc"><IMG ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A> is
<P><A NAME="eqnmodelsumc"> </A> <IMG WIDTH=500 HEIGHT=16 ALIGN=BOTTOM ALT="equation368" SRC="img28.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img28.gif" ><P>
where <IMG WIDTH=238 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline58233" SRC="img29.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img29.gif" >
and <IMG WIDTH=219 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline58235" SRC="img30.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img30.gif" >.
<P>
<HR><A NAME="tex2html2352" HREF="page38.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page38.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="next_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/next_motif.gif"></A> <A NAME="tex2html2350" HREF="page35.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page35.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="up_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/up_motif.gif"></A> <A NAME="tex2html2344" HREF="page36.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page36.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/previous_motif.gif"></A> <A NAME="tex2html2354" HREF="page9.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page9.html"><IMG WIDTH=65 HEIGHT=24 ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/contents_motif.gif"></A> <A NAME="tex2html2355" HREF="page620.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page620.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="index_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/index_motif.gif"></A> <P><ADDRESS>
<img src="bruno.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/bruno.gif" alt="Bruno" align=right>
<a href="javascript:if(confirm('http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html'" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html">Copyright © 1997</a> by <a href="javascript:if(confirm('http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html'" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html">Bruno R. Preiss, P.Eng.</a> All rights reserved.
</ADDRESS>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?