📄 page_251.html
字号:
</tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">6. The factorial of a number </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3"><i>n</i></font><font face="Times New Roman, Times, Serif" size="3"> is </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3"><i>n</i></font><font face="Times New Roman, Times, Serif" size="3"> * (</font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3"><i>n</i></font><font face="Times New Roman, Times, Serif" size="3">-1) * (</font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3"><i>n</i></font><font face="Times New Roman, Times, Serif" size="3">-2) * * 2 * 1. Stirling's formula approximates the factorial for large values of </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3"><i>n</i></font><i><font face="Times New Roman, Times, Serif" size="3">:</font></i></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"><img src="c1632a17d53dc6f59b852ac816d11149.gif" border="0" alt="0251-01.gif" width="68" height="37" /></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"><img src="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" border="0" width="29" height="1" alt="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" /></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">where </font><font face="Symbol" size="3">p</font><font face="Times New Roman, Times, Serif" size="3">=3.14159265 and <i>e</i>=2.718282.</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"><img src="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" border="0" width="29" height="1" alt="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" /></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">Write a C++ program that inputs an integer value (but stores it into a </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">float</font><font face="Times New Roman, Times, Serif" size="3"> variable </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">n</font><font face="Times New Roman, Times, Serif" size="3">), calculates the factorial of </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3"><i>n</i></font><font face="Times New Roman, Times, Serif" size="3"> using Stirling's formula, assigns the (rounded) result to a </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">long</font><font face="Times New Roman, Times, Serif" size="3"> integer variable, and then prints the result appropriately labeled.</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"><img src="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" border="0" width="29" height="1" alt="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" /></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">Depending on the value of<i>n,</i> you should obtain one of these results:</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"><img src="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" border="0" width="29" height="1" alt="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" /></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"> A numerical 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"><img src="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" border="0" width="29" height="1" alt="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" /></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"> If </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3"><i>n</i></font><font face="Times New Roman, Times, Serif" size="3"> equals 0, the factorial is defined to be 1.</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"><img src="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" border="0" width="29" height="1" alt="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" /></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"> If </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3"><i>n</i></font><font face="Times New Roman, Times, Serif" size="3"> is less than 0, the factorial is undefined.</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"><img src="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" border="0" width="29" height="1" alt="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" /></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"> If </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3"><i>n</i></font><font face="Times New Roman, Times, Serif" size="3"> is too large, the result exceeds </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">LONG_MAX</font><font face="Times New Roman, Times, Serif" size="3">.</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"><img src="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" border="0" width="29" height="1" alt="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" /></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">(</font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">LONG_MAX</font><font face="Times New Roman, Times, Serif" size="3"> is a constant declared in the header file </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">limits聽.h</font><font face="Times New Roman, Times, Serif" size="3">. It gives the maximum </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">long</font><font face="Times New Roman, Times, Serif" size="3"> value for your particular machine and C++ compiler.)</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"><img src="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" border="0" width="29" height="1" alt="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" /></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">Because Stirling's formula is used to calculate the factorial of very large numbers, the factorial will approach </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">LONG_MAX</font><font face="Times New Roman, Times, Serif" size="3"> quickly. If the factorial exceeds </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">LONG_MAX</font><font face="Times New Roman, Times, Serif" size="3">, it will cause an arithmetic overflow in the computer, in which case the program will either stop running or continue with a strange-looking integer result, perhaps negative. Before you write the program, then, you first must write a small program that lets you determine, by trial and error, the largest value of </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3"><i>n</i></font><font face="Times New Roman, Times, Serif" size="3"> for which your computer system can compute a factorial using Stirling's formula. After you've determined this value, you can write the program using nested Ifs that print different messages depending on the value of </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3"><i>n</i></font><i><font face="Times New Roman, Times, Serif" size="3">.</font></i><font face="Times New Roman, Times, Serif" size="3"> If n is within the acceptable range for your computer system, write the number and the result with an appropriate message. If </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3"><i>n</i></font><font face="Times New Roman, Times, Serif" size="3"> is 0, write the message, The number is 0. The factorial is 1.. If the number is less than 0, write The number is less than 0. The factorial is undefined.. If the number is greater than the largest value of </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3"><i>n</i></font><font face="Times New Roman, Times, Serif" size="3"> for which your computer system can compute a factorial, write The number is too large..</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_250.html">< previous page</a></td> <td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_251</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_252.html">next page ></a></td> </tr> </table> </body> </html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -