题解HJ72 | #百钱买百鸡问题#
百钱买百鸡问题
https://www.nowcoder.com/practice/74c493f094304ea2bda37d0dc40dc85b
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); String s = ""; while ((s = bf.readLine()) != null) { int num = Integer.parseInt(s); int x, y; for (int z = 0 ; z <= 100; z += 3) { x = 4 * z / 3 - 100; y = 200 - 7 * z / 3; if ( (x >= 0) && (x <= 100) && (y >= 0) && (y <= 100)) { System.out.println(x + " " + y + " " + z ); } } } } }
抄下代码 xyz三个未知数的规划问题 可以借鉴下
本质上就是解方程,满足约束条件 5x+3y+z/3 =100 以及 x+y+z=100;其中隐含条件是z必须是3的倍数,所以 x=4z/3 -100;y=200-7z/3;只需要遍历z,每次z加3,找到满足条件(0<=x<=100且0<=y<=100)的x和y就行,不需要判定是整数,因为z是3的倍数,所以x和y必定也是整数。(没有更进一步限定x和y的取值,交给计算机做吧)