【问题标题】:logic to write the function find_maximum_people编写函数 find_maximum_people 的逻辑
【发布时间】:2019-11-06 03:31:28
【问题描述】:

假设人体塔是在舞台上进行的,并且舞台有最大重量限制。 编写一个 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)
    

【问题讨论】:

  • 你能提供预期输出的东西吗?
  • 您是否尝试过自己解决作业?出了什么问题?
  • 实际上这是作业中的问题,所以我没有任何输出
  • 基地的最大人数只是最大人数下的第一个奇数? (1000 / 50 = 20) => 答案是 19。实际上不需要建造塔吗?
  • @Selcuk 你的建议奏效了

标签: python python-3.x logic


【解决方案1】:
    #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)

【讨论】:

    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 2018-12-27
      • 1970-01-01
      • 2021-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-17
      • 2020-02-03
      相关资源
      最近更新 更多