pythonExample100/JCP028.py

14 lines
481 B
Python
Raw Permalink Blame History

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

'''
【程序28】
题目有5个人坐在一起问第五个人多少岁他说比第4个人大2岁。问第4个人岁数他说比第
   3个人大2岁。问第三个人又说比第2人大两岁。问第2个人说比第一个人大两岁。最后
   问第一个人他说是10岁。请问第五个人多大
1.程序分析:利用递归的方法,递归分为回推和递推两个阶段。要想知道第五个人岁数,需知道
      第四人的岁数依次类推推到第一人10岁再往回推。
'''
def age(n):
if n == 1: c = 10
else: c = age(n - 1) + 2
return c
print age(5)