现在共有100匹马跟100块石头,马分3种,大型马;中型马跟小型马。其中一匹大马一次可以驮3块石头,中型马可以驮2块,而小型马2头可以驮一块石头。问三种马有多少种分配方案可以刚好用100匹马驮100块石头
Python代码如下:
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 9 19:15:34 2020
@author: yang
"""
#现共有100匹马跟100块石头,马分三种,大型马,中型马,小型马。
#其中一批大型马一次可以驼3块石头,中型马2块,小型马2头可以驼一块
#问需要多少匹大型马、中型马、小型马(必须用完100匹马)?
#数学模型:大型马A,中型马B,小型马C
#大型马驼运x,中型马y,小型马z
#输入:驮运能力
x = 3
y = 2
z = 0.5
#资源总数
Horse = 100
Stone = 100
A = 0
B = 0
C = 0
#
i = 0
j = 0
k = 0
#输出
for i in range(0,int(Horse/x)):
#if (A<100) and (A + B + C < 100) :
A = i
#print(A,B,C)
if (A + B + C == Horse) and (A*x + B*y + C*z ==Stone) :
print(A,B,C)
break
else:
for j in range(0,Horse-i):
#print(A,B,C)
#if (B<100) and (A + B + C < 100) and (A*x + B*y + C*z)<100:
B = j
if (A + B + C == Horse) and (A*x + B*y + C*z ==Stone) :
print(A,B,C)
break
else:
for k in range(0,Horse-i-j):
#if (C<100) and (A + B + C < 100) and (A*x + B*y + C*z)<100:
C = k
#print(A,B,C,i,j,k)
if (A + B + C == Horse) and (A*x + B*y + C*z ==Stone) :
print(A,B,C)
break
运行结果: