Python 编写函数find_max_people的逻辑
考虑到人塔将在舞台上进行,且舞台具有最大重量限制。 编写一个python程序,找出基本层的最大人数,这样塔楼的总重量就不会超过舞台的最大重量限制。 假设:Python 编写函数find_max_people的逻辑,python,python-3.x,logic,Python,Python 3.x,Logic,考虑到人塔将在舞台上进行,且舞台具有最大重量限制。 编写一个python程序,找出基本层的最大人数,这样塔楼的总重量就不会超过舞台的最大重量限制。 假设: 每人体重50公斤 人类之塔的底层总是有奇数人 每一级别的人数减少2人 def human_pyramid(no_of_people): if (no_of_people == 1): return 1 * (50) else: return no_of_people * (50) + human_pyr
def human_pyramid(no_of_people):
if (no_of_people == 1):
return 1 * (50)
else:
return no_of_people * (50) + human_pyramid(no_of_people - 2)
def find_maximum_people(max_weight):
pass
max_people = find_maximum_people(1000)
print(max_people)
#此函数返回塔的重量
def人类金字塔(没有人):
如果(人数=1):
返回1*(50)
其他:
返回人数*(50)+人数金字塔(人数-2)
def查找最大人数(最大体重):
i=1
而imax_重量:
返回i-1
#当重量超过此值时,表示旧值处于限制范围内
i=i+2
最大人数=查找最大人数(1000)
打印(最多人)
定义人类金字塔(没有人):
如果(人数=1):
返回1*(50)
其他:
返回人数*(50)+人数金字塔(人数-2)
def查找最大人数(最大体重):
最大=最大重量//50
i=最大值
你能提供预期的输出吗?你试过自己解决作业了吗?出了什么问题?实际上这是作业中的一个问题,所以我没有任何输出基地上的最大人数只是最大人数下的第一个奇数?(1000/50=20)=>答案是19。没有要求建造一座真正的塔吗?@Selcuk你的建议奏效了
#this funtion return the weight of the tower
def human_pyramid(no_of_people):
if (no_of_people == 1):
return 1 * (50)
else:
return no_of_people * (50) + human_pyramid(no_of_people - 2)
def find_maximum_people(max_weight):
i=1
while i<(max_weight//50):
current_weight=human_pyramid(i)
if current_weight>max_weight:
return i-1
#when the weight exceed this means older value was in the limit
i=i+2
max_people = find_maximum_people(1000)
print(max_people)
def human_pyramid(no_of_people):
if (no_of_people == 1):
return 1 * (50)
else:
return no_of_people * (50) + human_pyramid(no_of_people - 2)
def find_maximum_people(max_weight):
max=max_weight//50
i = max
while i<=max:
curr=human_pyramid(max)
max=max-2
i=i-2
max_people = find_maximum_people(1000)
print(max_people)