题目抽象:min1<=a<=max1, min2 <= b <= max2, min3 <= c <= max3; a+b+c = n. min1 +min2 + min3 <= n <= max1 + max2 + max3;
在满足上面的条件下,第一比较规则a尽可能大,第二比较规则b尽可能大。求出a,b,c;
分析: 贪心即可。分两类, a能否取到最大值, a取到最大值的话,b能否取到最大值。 见代码。
1 import java.io.*; 2 import java.util.*; 3 import java.math.*; 4 5 public class Main 6 { 7 static Scanner cin = new Scanner(new BufferedInputStream(System.in)); 8 static PrintStream cout = System.out; 9 10 public static void main(String[] args) 11 { 12 int n = cin.nextInt(); 13 int min1 = cin.nextInt(), max1 = cin.nextInt(); 14 int min2 = cin.nextInt(), max2 = cin.nextInt(); 15 int min3 = cin.nextInt(), max3 = cin.nextInt(); 16 17 if(max1 + min2 + min3 <= n) 18 { 19 if(max1 + max2 +min3 <= n) 20 System.out.println(max1 + " " + max2 + " " +(n - max1 - max2)); 21 else 22 System.out.println(max1 + " " + (n - max1 - min3) + " " + min3); 23 } 24 else 25 { 26 System.out.println((n - min2 - min3) + " " + min2 + " " + min3); 27 } 28 } 29 }
时间: 2024-12-30 01:41:29