Java做的,代码结构没有丝毫优化,交流一下思路好了。。
1.喝咖啡调试bug。 就把所有bug加起来求的。。没感觉有啥难以调试的点
import java.util.Scanner; public class dajiang1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int N = in.nextInt(); int A = in.nextInt(); int X = in.nextInt(); int total = 0; int resultTime = 0; for (int i = 0; i < N; i++) { total += in.nextInt(); } int temp = X; for (int i = 0; i < X; i++) { if(total - A*60 > 0){ total -= A*60; resultTime += 60; temp--; } } if(temp > 0){ resultTime += Math.ceil((double)total/A); System.out.println(resultTime); }else{ if(total > 480 - X*60){ System.out.println(0); return; }else{ System.out.println(resultTime + total); } } } }2。吃零食,动态规划,更新时判断零食还够不够
import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; class food{ int price; int satisfy; int num; public food(int price, int satisfy, int num) { this.price = price; this.satisfy = satisfy; this.num = num; } } class account { int totalStatisfy; int[] numbers; public account(int totalStatisfy) { this.totalStatisfy = totalStatisfy; } } public class dajiang2 { public static int N; public static void main(String[] args) { Scanner in = new Scanner(System.in); N = in.nextInt(); int T = in.nextInt(); account[] moneyMax = new account[T+1]; food[] foods = new food[N]; int result = 0; for (int i = 0; i < N; i++) { foods[i] = new food(in.nextInt(), in.nextInt(), in.nextInt()); } moneyMax[0] = new account(0); moneyMax[0].numbers = new int[N]; for (int i = 0; i < N; i++) { moneyMax[0].numbers[i] = foods[i].num; } for (int i = 1; i <= T; i++) { findMax(moneyMax, i, foods); if(moneyMax[i].totalStatisfy > result) result = moneyMax[i].totalStatisfy; } System.out.println(result); } public static void findMax(account[] moneyMax, int point, food[] foods){ int max = 0; moneyMax[point] = new account(0); moneyMax[point].numbers = new int[N]; for (int i = 0; i < N; i++) { moneyMax[point].numbers[i] = foods[i].num; } for (int i = 0; i < foods.length; i++) { boolean whetherBuy = false; if(point - foods[i].price >= 0 && moneyMax[point - foods[i].price].numbers[i] > 0){ if(moneyMax[point - foods[i].price].totalStatisfy + foods[i].satisfy > max){ max = moneyMax[point - foods[i].price].totalStatisfy + foods[i].satisfy; moneyMax[point].totalStatisfy = max; for (int j = 0; j < N; j++) { moneyMax[point].numbers[j] = moneyMax[point - foods[i].price].numbers[j]; } moneyMax[point].numbers[i]--; } } } } }3.字母表,递归。之前是64%。因为递归的时候漏了二维数组一些边界条件的判断,修改完不知道能有多少
import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class dajiang3 { public static boolean flag = false; public static void main(String[] args) { Scanner in = new Scanner(System.in); int N = in.nextInt(); in.nextLine(); char[][] charArray = new char[N][]; for (int i = 0; i < N; i++) { String temp = in.nextLine(); charArray[i] = temp.toCharArray(); } int M = in.nextInt(); in.nextLine(); String[] stringArray = new String[M]; ArrayList<String> resultArray = new ArrayList<>(); for (int i = 0; i < M; i++) { stringArray[i] = in.nextLine(); flag = false; for (int j = 0; j < charArray.length; j++) { for (int k = 0; k < charArray[j].length; k++) { if(charArray[j][k] == stringArray[i].charAt(0)){ ifOk(stringArray[i], 1, charArray, j, k); } } } if(flag) resultArray.add(stringArray[i]); } //之前用牛客考试的时候,有一次自定义ArrayList比较器,使用Colletions为ArrayList排序,自己IDE没问题,牛客显示数组越界,不知道为啥,有没有大佬告诉一下原因。。。 //所以现在考试排序都是复制到数组里用Arrays排序。 String[] result = new String[resultArray.size()]; for (int i = 0; i < resultArray.size(); i++) { result[i] = resultArray.get(i); } Arrays.sort(result); for (int i = 0; i < result.length; i++) { System.out.println(result[i]); } } public static void ifOk(String string, int place, char[][] charArray, int i, int j){ if(place == string.length()){ flag = true; return; }else{ if(i - 1 > 0 && j < charArray[i-1].length && charArray[i-1][j] == string.charAt(place)){ ifOk(string, place+1, charArray, i-1, j); } if(i + 1 < charArray.length && j < charArray[i+1].length && charArray[i+1][j] == string.charAt(place)){ ifOk(string, place+1, charArray, i+1, j); } if(j - 1 > 0 && i < charArray.length && charArray[i][j-1] == string.charAt(place)){ ifOk(string, place+1, charArray, i, j - 1); } if(j + 1 <charArray[i].length && i < charArray.length && charArray[i][j+1] == string.charAt(place)){ ifOk(string, place+1, charArray, i, j + 1); } } } }
全部评论
(7) 回帖