写函数find\u maximum\u peop的逻辑

2024-10-06 11:44:01 发布

您现在位置:Python中文网/ 问答频道 /正文

考虑到人塔将在舞台上进行,并且舞台有最大重量限制。 编写一个python程序,求出底层的最大人数,使塔楼的总重量不超过舞台的最大重量限制。 假设:

  1. 每人重50公斤。你知道吗
  2. 人类之塔的底层总是有奇数人。你知道吗
  3. 每一级的人数减少2人。你知道吗

    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)
    

Tags: ofnopyramidreturndeffindpeoplemax
2条回答
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)
    #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)

相关问题 更多 >