Python计数牲畜数量

时间:2015-03-28 20:59:30

标签: python python-2.7

为了说明这一点,我们有两种动物,一种是猪,另一种是鸡。用户输入这两种特定类型动物的头部和腿部的数量。例如,如果我们运行这个并输入20个头和56个腿,我们得到12只鸡和8只猪。看看这里:

def solve(numLegs, numHeads):
    for numChicks in range(0, numHeads + 1): # how we get 12 chickens here?
        numPigs = numHeads - numChicks # numpigs = 20 - 12 = 8 ?
        totLegs = 4 * numPigs + 2* numChicks # 4 * 8 + 2 * 12 ?
        if totLegs == numLegs:
           return [numPigs, numChicks]
    return[None, None]

def barnYard():
    heads = int(raw_input('Enter number of heads:'))
    legs = int(raw_input('Enter number of legs:'))
    pigs, chickens = solve(legs, heads)
    if pigs = None:
       print 'there is no solution'
    else:
        print 'number of pigs:' , pigs
        pirnt 'number of chickes:', chickens

我们计算totLegs = 4 * numPigs + 2* numChicks知道猪有4条腿,鸡有2条,后来验证这是否符合输入的腿数?但问题是for numChicks in range(0, numHeads + 1):如何发现鸡的数量是12?如果没有找到鸡的数量,如果我没有错,我们就无法继续下一行。你能解释一下吗?这让我很困惑。代数版本可以在这里找到。 link

1 个答案:

答案 0 :(得分:-2)

Don't make your life more complicated than it already is,只需使用print command to see the intermediate valuessimple algebra。顺便说一句,您的代码中包含many errorsdoesn't even compile(大多数为typos,例如pirnt而不是print)。请参见fixed code which works:

def solve(numLegs, numHeads):
    for numChicks in range(0, numHeads + 1): #we have got 12 chickens here?
        print "numChicks = ", numChicks
        numPigs = numHeads - numChicks
        print "numPigs = ", numPigs
        totLegs = 4 * numPigs + 2* numChicks
        print "totLegs = ",totLegs
        if totLegs == numLegs:
            print "numLegs = ",numLegs
            print "numPigs = ",numPigs
            print "numChicks = ",numChicks
            return [numPigs, numChicks]
    return[None, None]

def barnYard():
    heads = int(raw_input('Enter number of heads:'))
    legs = int(raw_input('Enter number of legs:'))
    pigs, chickens = solve(legs, heads)
    if pigs == None:
       print 'there is no solution'
    else:
        print 'number of pigs:' , pigs
        print 'number of chicken:', chickens



if __name__ == "__main__":
    barnYard()

输出

Enter number of heads:20
Enter number of legs:56
numChicks =  0
numPigs =  20
totLegs =  80
numChicks =  1
numPigs =  19
totLegs =  78
numChicks =  2
numPigs =  18
totLegs =  76
numChicks =  3
numPigs =  17
totLegs =  74
numChicks =  4
numPigs =  16
totLegs =  72
numChicks =  5
numPigs =  15
totLegs =  70
numChicks =  6
numPigs =  14
totLegs =  68
numChicks =  7
numPigs =  13
totLegs =  66
numChicks =  8
numPigs =  12
totLegs =  64
numChicks =  9
numPigs =  11
totLegs =  62
numChicks =  10
numPigs =  10
totLegs =  60
numChicks =  11
numPigs =  9
totLegs =  58
numChicks =  12
numPigs =  8
totLegs =  56
numLegs =  56
numPigs =  8
numChicks =  12
number of pigs: 8
number of chicken: 12

这是简单的数学。 You have 20 heads, 56 legs. every pigs has 4 legs, every chicken 2 legs

所以

56 = 4*X+2*Y
   X+Y=20

这意味着X = 20 - Y

然后56 = 4*(20-Y)+2*Y => 56 = 80 -4Y+2Y => 2Y = 80 - 56 => 2Y = 24 => Y = 12 Chicken

然后X + 12 = 20 => X = 8 Pigs

:d

希望它有所帮助!!