第一题AC,注意点:输入是long类型,不能用BufferedReader,不然很麻烦:
1 public class Main { 2 3 public static void main(String[] args) throws IOException { 4 Scanner in = new Scanner(System.in); 5 long x = in.nextLong(); 6 long f = in.nextLong(); 7 long d = in.nextLong(); 8 long p = in.nextLong(); 9 long last = d - x * f; 10 if(last > 0) { 11 long day = last / (x + p); 12 System.out.println(day + f); 13 } 14 //如果钱还不够付初始房费的 15 else { 16 long day = d / x; 17 System.out.println(day); 18 } 19 } 20 21 }
第二题过了60%,穷举所有可能性。先计算单独吃一盒三份、一盒两份和一盒一份可维持的天数,然后再将剩余的数量拼凑起来。不知道哪里错了。代码如下:
1 public class Main { 2 public static void main(String[] args) { 3 Scanner in = new Scanner(System.in); 4 int t = in.nextInt(); 5 while(t-- != 0) { 6 int n = in.nextInt(); 7 int a = in.nextInt(); 8 int b = in.nextInt(); 9 int c = in.nextInt(); 10 int day_c = c / 2, last_c = c % 2; 11 int day_b = b / 3, last_b = b % 3; 12 int day_a = a / 6, last_a = a % 6; 13 int last = 0; 14 //三份还剩一盒 15 if(last_c == 1) { 16 //两份还剩一盒 17 if(last_b == 1) { 18 //3+2+1 19 if(last_a > 0) { 20 last = 1; 21 } 22 } 23 //两份还剩两盒 24 else if(last_b == 2) { 25 //3+2+1 26 if(last_a > 0 && last_a < 5) { 27 last = 1; 28 } 29 //3+4+5 30 else if(last_a == 5) { 31 last = 2; 32 } 33 } 34 else { 35 //3+0+3 36 if(last_a >= 3) { 37 last = 1; 38 } 39 } 40 } 41 else { 42 //0+2+4 43 if(last_b == 1) { 44 if(last_a >= 4) { 45 last = 1; 46 } 47 } 48 //0+4+2 49 else if(last_b == 2) { 50 if(last_a >= 2) { 51 last = 1; 52 } 53 } 54 } 55 // System.out.println(day_c + "," + day_b + "," + day_a + "," + last); 56 int day = day_c + day_b + day_a + last; 57 if(day >= n) { 58 System.out.println("Yes"); 59 } 60 else { 61 System.out.println("No"); 62 } 63 } 64 } 65 }
据说别人AC的思路,也是穷举,只是先单独吃一盒三份和一盒两份的,然后再将剩余的与一盒一份的进行拼凑,最后单独吃一盒一份的。代码如下:
1 public class Main2 { 2 3 public static void main(String[] args) { 4 Scanner in = new Scanner(System.in); 5 int t = in.nextInt(); 6 while(t-- != 0) { 7 int n = in.nextInt(); 8 int a = in.nextInt(); 9 int b = in.nextInt(); 10 int c = in.nextInt(); 11 int day_c = c / 2, last_c = c % 2; 12 int day_b = b / 3, last_b = b % 3; 13 int day = 0; 14 //三份还剩一盒 15 if(last_c == 1) { 16 //两份还剩一盒 17 if(last_b == 1) { 18 //3+2+1 19 if(a >= 1) { 20 day++; 21 a -= 1; 22 } 23 } 24 //两份还剩两盒 25 else if(last_b == 2) { 26 //3+2+1 27 if(a >= 1 && a < 5) { 28 a -= 1; 29 day++; 30 } 31 //3+4+5 32 else if(a >= 5) { 33 a -= 5; 34 day += 2; 35 } 36 } 37 else { 38 //3+0+3 39 if(a >= 3) { 40 a -= 3; 41 day++; 42 } 43 } 44 } 45 else { 46 //0+2+4 47 if(last_b == 1) { 48 if(a >= 4) { 49 a -= 4; 50 day++; 51 } 52 } 53 //0+4+2 54 else if(last_b == 2) { 55 if(a >= 2) { 56 a -= 2; 57 day++; 58 } 59 } 60 } 61 day = day + day_c + day_b + a / 6; 62 if(day >= n) { 63 System.out.println("Yes"); 64 } 65 else { 66 System.out.println("No"); 67 } 68 } 69 } 70 71 }