4、标题:判断一组不等式是否满足约束并输出最大差
【判断一组不等式是否满足约束并输出最大差】给定一组不等式,判断是否成立并输出不等式的最大差(输出浮点数的整数部分)
要求:
1)不等式系数为 double类型,是一个二维数组
2)不等式的变量为 int类型,是一维数组;
3)不等式的目标值为 double类型,是一维数组
4)不等式约束为字符串数组,只能是:">",">=","<","=",
例如,不等式组:
a11x1+a12x2+a13x3+a14x4+a15x5<=b1; a21x1+a22x2+a23x3+a24x4+a25x5<=b2; a31x1+a32x2+a33x3+a34x4+a35x5<=b3;
最大差
=max{(a11x1+a12x2+a13x3+a14x4+a15x5-b1),(a21x1+a22x2+a23x3+a24x4+ a25x5-b2),(a31x1+a32x2+a33x3+a34x4+a35x5-b3)},类型为整数(输出浮点数的整数部分)
输入描述:
1)不等式组系数(double类型):
a11,a12,a13,a14,a15
a21,a22,a23,a24,a25
a31,a32,a33,a34,a35
2)不等式变量(int类型):
x1,x2,x3,x4,x5 3)不等式目标值(double类型):b1,b2,b3 4)不等式约束(字符串类型):<=,<=,<=
输入:
a11,a12,a13,a14,a15,a21,a22,a23,a24,a25, a31,a32,a33,a34,a35,x1,x2,x3,x4,x5,b1,b2,b3,<=,<=,<=
输出描述:true或者 false,最大差
示例:
输入
2.3,3,5.6,7,6;11,3,8.6,25,1;0.3,9,5.3,66,7.8;1,3,2,7,5;340,670,80.6;<=,<=,<=
输出
false,458
def max_difference(str):
l = str.split(";")
a1 = [x.split(',') for x in l[-3:]]
a2 = l[:-3]
b = []
for i in range(len(a2)):
c = a2[i].split(',')
s = 0
for j in range(len(c)):
s += float(c[j]) * float(a1[0][j])
b.append([s, a1[-1][i], float(a1[-2][i])])
return all([eval('%s %s %s' % (x[0], x[1], x[2])) for x in b]), int(max([x[0] - x[-1] for x in b]))
print(max_difference("2.3,3,5.6,7,6;11,3,8.6,25,1;0.3,9,5.3,66,7.8;1,3,2,7,5;340,670,80.6;<=,<=,<="))