项目 2 人类金字塔计算 [英] Project 2 Human Pyramid Calculations

查看:51
本文介绍了项目 2 人类金字塔计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为简单起见,我们假设金字塔中的每个人的体重恰好为 200 磅.人金字塔顶端的A背上没有重量.B 和 C 各携带一半人A的体重.这意味着他们每个人都背负着 100 磅.现在,让我们看看第三排的人.让我们从关注人 E 开始. 多少她在支撑体重吗?嗯,她直接支撑了 B 人一半的重量(100磅)和人 E 体重的一半(100 磅),所以她至少支撑着 200 磅.最重要的是,她感受到了 B 和 C 所承受的一些重量.一半的人 B 所承受的重量(50 磅)传递给人 E 和一半人 C 所承受的重量(50 磅)同样会传递给人 E,所以人 E 最终感觉体重增加了 100 磅.这意味着她总共支持 300磅.

For simplicity we will assume that everyone in the pyramid weighs exactly 200 pounds. Person A at the top of the pyramid has no weight on her back. People B and C are each carrying half of person A's weight. That means that each of them is shouldering 100 pounds. Now, let's look at the people in the third row. Let’s begin by focusing on person E. How much weight is she supporting? Well, she’s directly supporting half the weight of person B (100 pounds) and half the weight of person E (100 pounds), so she’s supporting at least 200 pounds. On top of this, she’s feeling some of the weight that people B and C are carrying. Half of the weight that person B is shouldering (50 pounds) gets transmitted down onto person E and half the weight that person C is shouldering (50 pounds) similarly gets sent down to person E, so person E ends up feeling an extra 100 pounds. That means she’s supporting a net total of 300 pounds.

编写一个递归函数(不使用循环),weightOn(r,c),它返回权重在第 r 行和第 c 列的人的背面.行和列是从 0 开始的,例如,顶部位置是 (0,0),而人 H 位于位置 (3,1).以下还持有:weightOn(0,0) == 0.00weightOn(3,1) == 425.00权重应该是浮点数.

Write a recursive function (using no loops), weightOn(r,c), which returns the weight on the back of the person in row r and and column c. Rows and columns are 0-based, so the top position is (0,0), for example, and person H is in position (3,1). The following also hold: weightOn(0,0) == 0.00 weightOn(3,1) == 425.00 Weights should be floating-point numbers.

我已经尝试了很多.我将在下面包含我最近的代码.

I have already tried a lot. I will include my most recent code below.

t = 0.0
x = 0.0

def weightOn(r, c):

    global t
    if r < 0:
        print('Not valid')
    elif r == 0 and c == 0:
        return t
    elif r > 0 and c == 0:
        t += 200 / (2 ** r)
        return weightOn(r - 1, 0)
    elif r > 0 and c == r:
        t += 200 / (2 ** r)
        return weightOn(r - 1, 0)
    elif r > c > 0:
        mid(r, c)
        return t

def mid(r, c):

    global x
    x = weightOn(r - 1, c - 1) + weightOn(r - 1, c)
'''I have also tried: x = (((weightOn(r - 1, c - 1) + 200) / 2) + ((weightOn(r - 1, c) + 200) / 2))'''
    return x

r = int(input('r: '))
c = int(input('c: '))
weightOn(r, c)
if r > c > 0:
    print(x)
else:
    print(t)

它总是带来错误的输出.我可以正确地拉起所有边缘(当 c == 0 或 c == r 时).但除此之外,它不起作用.

It always brings up the wrong output. I can correctly pull up all of the edges (when c == 0 or c == r). But other than that it won't work.

例如.输入 (3, 1) 输出 500(3, 2) 输出 55​​0

Ex. Input (3, 1) outputs 500 (3, 2) outputs 550

推荐答案

使用全局变量表明您没有递归考虑这一点.

Using global variables suggests that you haven't considered this recursively.

每个人承担每个肩膀上的人的一半重量.每个人的有效体重是他们肩负的重量,再加上 200 磅.如果一个人在边缘,那么另一个肩膀上的人"的权重为 0.

Each person shoulders half the weight of the persons on each shoulder. The effective weight of each person is what they shoulder, plus 200 pounds. If a person is on the edge, then the "person" on the other shoulder has 0 weight.

所以……

def weight(r, c):
# Code base cases
if r < 0:            # Past the pyramid top; no such person
    return 0
if c < 0 or c > r:   # Off the edge; no such person
    return 0
return 200 + (weight(r - 1, c - 1) + weight(r - 1, c)) / 2

那么weightOn就是上面的例程,没有200 +.

Then weightOn is simply the above routine without the 200 +.

这是你的大纲;你能从那里拿走吗?

That's your outline; can you take it from there?

这篇关于项目 2 人类金字塔计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆