⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 page_1125.html

📁 Programming and Problem Solving with C++
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">	<html>		<head>			<title>page_1125</title>			<link rel="stylesheet" href="reset.css" type="text/css" media="all">			<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />		</head>		<body>		<table summary="top nav" border="0" width="100%">			<tr>				<td align="left" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_1124.html">&lt;&nbsp;previous page</a></td>				<td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_1125</strong></td>				<td align="right" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_1126.html">next page&nbsp;&gt;</a></td>			</tr>					<tr>				<td align="left" colspan="3" style="background: #ffffff; padding: 20px;">    <table border="0" width="100%" cellpadding="0"><tr><td align="center">  <table border="0" cellpadding="2" cellspacing="0" width="100%"><tr><td align="left"></td>  <td align="right"></td>  </tr></table></td></tr><tr><td align="left"><p></p><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td align="right"><font face="Times New Roman, Times, Serif" size="2" color="#FF0000">Page 1125</font></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td>  <td colspan="3" height="12"></td>  <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td>  <td><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">int聽Factorial聽(聽/*聽in聽*/聽int聽n聽)<br /><br />//聽Precondition:<br />//聽聽聽聽聽n聽&gt;=聽0<br />//聽Postcondition:<br />//聽聽聽聽聽Function聽value聽==聽n!<br />//聽Note:<br />//聽聽聽聽聽Large聽values聽of聽n聽may聽cause聽integer聽overflow<br /><br />{<br />聽聽聽聽if聽(n聽==聽0)<br />聽聽聽聽聽聽聽聽return聽1;聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽//聽Base聽case<br />聽聽聽聽else<br />聽聽聽聽聽聽聽聽return聽n聽*聽Factorial(n聽-聽1);聽聽聽聽聽聽聽//聽General聽case<br />}</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td>  <td colspan="3" height="12"></td>  <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td>  <td><font face="Times New Roman, Times, Serif" size="3">Let's trace this function with an original </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">n</font><font face="Times New Roman, Times, Serif" size="3"> of 4.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td>  <td colspan="3" height="12"></td>  <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td>  <td><font face="Times New Roman, Times, Serif" size="3"><i>Call 1:</i> </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">n</font><font face="Times New Roman, Times, Serif" size="3"> is 4. Because </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">n</font><font face="Times New Roman, Times, Serif" size="3"> is not 0, the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">else</font><font face="Times New Roman, Times, Serif" size="3"> branch is taken. The </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">return</font><font face="Times New Roman, Times, Serif" size="3"> statement cannot be completed until the recursive call to </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Factorial</font><font face="Times New Roman, Times, Serif" size="3"> with n - 1 as the actual parameter has been completed.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td>  <td colspan="3" height="12"></td>  <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td>  <td><font face="Times New Roman, Times, Serif" size="3"><i>Call 2:</i> </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">n</font><font face="Times New Roman, Times, Serif" size="3"> is 3. Because </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">n</font><font face="Times New Roman, Times, Serif" size="3"> is not 0, the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">else</font><font face="Times New Roman, Times, Serif" size="3"> branch is taken. The </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">return</font><font face="Times New Roman, Times, Serif" size="3"> statement cannot be completed until the recursive call to </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Factorial</font><font face="Times New Roman, Times, Serif" size="3"> with </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">n</font><font face="Times New Roman, Times, Serif" size="3"> - 1 as the actual parameter has been completed.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td>  <td colspan="3" height="12"></td>  <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td>  <td><font face="Times New Roman, Times, Serif" size="3"><i>Call 3:</i> </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">n</font><font face="Times New Roman, Times, Serif" size="3"> is 2. Because </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">n</font><font face="Times New Roman, Times, Serif" size="3"> is not 0, the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">else</font><font face="Times New Roman, Times, Serif" size="3"> branch is taken. The </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">return</font><font face="Times New Roman, Times, Serif" size="3"> statement cannot be completed until the recursive call to </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Factorial</font><font face="Times New Roman, Times, Serif" size="3"> with </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">n</font><font face="Times New Roman, Times, Serif" size="3"> - 1 as the actual parameter has been completed.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td>  <td colspan="3" height="12"></td>  <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td>  <td><font face="Times New Roman, Times, Serif" size="3"><i>Call 4:</i> </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">n</font><font face="Times New Roman, Times, Serif" size="3"> is 1. Because </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">n</font><font face="Times New Roman, Times, Serif" size="3"> is not 0, the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">else</font><font face="Times New Roman, Times, Serif" size="3"> branch is taken. The </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">return</font><font face="Times New Roman, Times, Serif" size="3"> statement cannot be completed until the recursive call to </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Factorial</font><font face="Times New Roman, Times, Serif" size="3"> with </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">n</font><font face="Times New Roman, Times, Serif" size="3"> - 1 as the actual parameter has been completed.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td>  <td colspan="3" height="12"></td>  <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td>  <td><font face="Times New Roman, Times, Serif" size="3"><i>Call 5:</i> </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">n</font><font face="Times New Roman, Times, Serif" size="3"> is 0. Because </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">n</font><font face="Times New Roman, Times, Serif" size="3"> equals 0, this call to the function returns, sending back 1 as the result.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td>  <td colspan="3" height="12"></td>  <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td>  <td><font face="Times New Roman, Times, Serif" size="3"><i>Call 4:</i> The </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">return</font><font face="Times New Roman, Times, Serif" size="3"> statement in this copy can now be completed. The value to be returned is </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">n</font><font face="Times New Roman, Times, Serif" size="3"> (which is 1) times 1. This call to the function returns, sending back 1 as the result.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td>  <td colspan="3" height="12"></td>  <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td>  <td><font face="Times New Roman, Times, Serif" size="3"><i>Call 3:</i> The </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">return</font><font face="Times New Roman, Times, Serif" size="3"> statement in this copy can now be completed. The value to be returned is </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">n</font><font face="Times New Roman, Times, Serif" size="3"> (which is 2) times 1. This call to the function returns, sending back 2 as the result.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td>  <td colspan="3" height="12"></td>  <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td>  <td><font face="Times New Roman, Times, Serif" size="3"><i>Call 2:</i> The </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">return</font><font face="Times New Roman, Times, Serif" size="3"> statement in this copy can now be completed. The value to be returned is </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">n</font><font face="Times New Roman, Times, Serif" size="3"> (which is 3) times 2. This call to the function returns, sending back 6 as the result.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td>  <td colspan="3" height="12"></td>  <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td>  <td><font face="Times New Roman, Times, Serif" size="3"><i>Call 1:</i> The </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">return</font><font face="Times New Roman, Times, Serif" size="3"> statement in this copy can now be completed. The value to be returned is </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">n</font><font face="Times New Roman, Times, Serif" size="3"> (which is 4) times 6. This call to the function returns, sending back 24 as the result. Because this is the last of the calls to </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Factorial</font><font face="Times New Roman, Times, Serif" size="3">, the recursive process is over. The value 24 is returned as the final value of the call to </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Factorial</font><font face="Times New Roman, Times, Serif" size="3"> with an actual parameter of 4. Figure 19-3</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table></td></tr></table><p><font size="0"></font></p>  </td>			</tr>				<tr>				<td align="left" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_1124.html">&lt;&nbsp;previous page</a></td>				<td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_1125</strong></td>				<td align="right" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_1126.html">next page&nbsp;&gt;</a></td>			</tr>		</table>		</body>	</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -