File tree Expand file tree Collapse file tree 3 files changed +61
-1
lines changed Expand file tree Collapse file tree 3 files changed +61
-1
lines changed Original file line number Diff line number Diff line change @@ -190,8 +190,45 @@ OK!完美地解决了问题,去除了code前面的一个空格。
190190
191191保存运行之,看看结果和你推算的是否一致。
192192
193+ ##练习5
194+
195+ ** 问题描述**
196+
197+ 在数学中,用n!来表示阶乘。例如,4!=1×2×3×4=24。如果将n个物体排列,有多少种排列方式,那就是n!。根据定义,0!=1。
198+
199+ ** 解析**
200+
201+ 下面用Python程序来计算阶乘。
202+
203+ #!/usr/bin/env python
204+ # coding=utf-8
205+
206+ n = int(raw_input("Enter an interger >=0: "))
207+
208+ fact = 1
209+
210+ for i in range(2, n + 1):
211+ fact = fact * i
212+
213+ print str(n) + " factorial is " + str(fact)
214+
215+ 这是用for循环来实现的,当然,你也可以使用while循环来计算阶乘。其代码如下:
216+
217+ #!/usr/bin/env python
218+ # coding=utf-8
219+
220+ n = int(raw_input('Enter an integer >= 0: '))
221+
222+ fact = 1
223+ i = 2
224+ while i<=n:
225+ fact = fact * i
226+ i += 1
227+
228+ print str(n) + " factorial is " + str(fact)
229+
193230------
194231
195232[ 总目录] ( ./index.md )   ;  ;  ; |  ;  ;  ; [ 上节:迭代] ( ./128.md )   ;  ;  ; |  ;  ;  ; [ 下节:自省] ( ./130.md )
196233
197- 如果你认为有必要打赏我,请通过支付宝:
** [email protected] ** , 不胜感激。
234+ 如果你认为有必要打赏我,请通过支付宝:
** [email protected] ** ,微信号: *** qiwsir * 不胜感激。
Original file line number Diff line number Diff line change 1+ #!/usr/bin/env python
2+ # coding=utf-8
3+
4+ n = int (raw_input ("Enter an interger >=0: " ))
5+
6+ fact = 1
7+
8+ for i in range (2 , n + 1 ):
9+ fact = fact * i
10+
11+ print str (n ) + " factorial is " + str (fact )
Original file line number Diff line number Diff line change 1+ #!/usr/bin/env python
2+ # coding=utf-8
3+
4+ n = int (raw_input ('Enter an integer >= 0: ' ))
5+
6+ fact = 1
7+ i = 2
8+ while i <= n :
9+ fact = fact * i
10+ i += 1
11+
12+ print str (n ) + " factorial is " + str (fact )
You can’t perform that action at this time.
0 commit comments