|
发表于 2017-9-29 18:46:37
|
显示全部楼层
这类题都是直线,基本上就是求所有交点代入的最大值
用矩阵求解线性方程组,结果应该很精确
- import itertools as it
- import numpy as np
- def fun():
- result = []
- for i,j in it.combinations([((2,-1),2),((3,-2),3),((1,1),2)], 2):
- a = np.array(i[0]+j[0]).reshape(2,2)
- b = np.array([i[1],j[1]]).reshape(-1,1)
- result.append(*np.linalg.solve(a,b).T)
- # 去掉不符合项
- result = filter(lambda x: 2*x[0]-x[1] <= 2 and 3*x[0]-2*x[1] >= 3 and x[0]+x[1] <= 2, result)
- d = max(result, key=lambda x: x[0] + 2*x[1])
- return "最大值是:%s,当前(x,y)为:%s" % (d[0] + d[1] *2, d)
复制代码 |
|