大疆8.16 软件类B卷 64 100 27
三道题分别A了64 100 27,还是太菜了
1. 图的最短路径,直接上迪杰斯特拉 A了64,不知道为什么。。求大佬指点
2. 经背包,直接AC
public class Main2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int bag = sc.nextInt(); int i = 0, j = 0; int[] values = new int[N]; int[] times = new int[N]; while(N > 0) { values[i++] = sc.nextInt(); times[j++] = sc.nextInt(); N--; } System.out.println(maxValue(values, times, bag)); } public static int maxValue(int[] values, int[] times, int bag) { int[][] dp = new int[times.length + 1][bag + 1]; for (int i = times.length - 1; i >= 0; i--) { for (int j = bag; j >= 0; j--) { dp[i][j] = dp[i + 1][j]; if (j + times[i] <= bag) { dp[i][j] = Math.max(dp[i][j], values[i] + dp[i + 1][j + times[i]]); } } } return dp[0][0]; } }
3. 给定一个整数,去掉K位后使得留下的整数最小。时间不够了,直接暴力贪心的删除每一位,只过了27
public class Main3 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String Snum = sc.next(); int deLen = sc.nextInt(); for (int i = 0; i < deLen; i++) { int len = Snum.length(); for (int j = 0; j < len; j++) { if(Snum.charAt(j + 1) < Snum.charAt(j)) { Snum = Snum.substring(0, j) + Snum.substring(j + 1); break; } } } Integer num = Integer.parseInt(Snum); if (num == 0) { System.out.println(0); } else { System.out.println(Snum); } } }