文远知行---8/17 java开发笔试题
第一题 AC10% 数组越界。
package LinkCode; import java.util.Scanner; public class wenyuan { // 第1题 public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan = new Scanner(System.in); StringBuilder sb = new StringBuilder(); String line = ""; while (true) { line = scan.nextLine(); if (!line.endsWith(",")) { break; } sb.append(line.trim()); } String[] strs = sb.toString().replaceAll(" ", "").split(","); int len=strs.length; int[] input = new int[len]; for (int i = 0; i < len; i++) { input[i] = Integer.parseInt(strs[i]); } System.out.println(maxArea(input)); scan.close(); } public static int maxArea(int[] heights) { int i = 0; int j = heights.length - 1; int result = 0; while (i < j) { int area = (j - i) * Math.min(heights[i], heights[j]); result = Math.max(result, area); if (heights[i] <= heights[j]) { i++; } else { j--; } } return result; } }第二题 AC 100%
package LinkCode; import java.util.Scanner; public class wenyuan { //第二题 public static void main(String[] args) { Scanner sc = new Scanner(System.in); String string = sc.nextLine(); int sum = sc.nextInt(); String str = string.replaceAll(" ", ""); String[] temp = str.split(","); int[] coin = new int[Integer.MAX_VALUE]; for (int i = 0; i < temp.length; i++) coin[i] = Integer.parseInt(temp[i]); System.out.println(coinChange(coin, sum)); sc.close(); } public static int coinChange(int[] coins, int target) { if (target == 0) return 0; int[] dp = new int[target + 1]; for (int i = 1; i <= target; i++) { dp[i] = Integer.MAX_VALUE; for (int temp : coins) { if (i >= temp && dp[i - temp] != Integer.MAX_VALUE) { dp[i] = Math.min(dp[i], dp[i - temp] + 1); } } } if (dp[target] < Integer.MAX_VALUE && dp[target] > 0) return dp[target]; else return -1; } }第三题 AC 8.333% 数组越界
package LinkCode; import java.util.Arrays; import java.util.Scanner; public class wenyuan { //第3题 public static void main(String args[]) { Scanner scan = new Scanner(System.in); StringBuilder sb = new StringBuilder(); String line = ""; while (true) { line = scan.nextLine(); if (!line.endsWith(",")) { break; } sb.append(line.trim()); } String[] strs = sb.toString().replaceAll(" ", "").split(","); long[] input = new long[strs.length]; for (int i = 0; i < strs.length; i++) { input[i] = Integer.parseInt(strs[i]); } System.out.println(leap(input)); scan.close(); } public static int leap(long[] arr) { int[] res = new int[arr.length]; Arrays.fill(res, Integer.MAX_VALUE - 1); res[0] = 0; for (int i = 0; i < arr.length; i++) { for (int j = 1; j < arr[i] + 1; j++) { if (i + j < arr.length) { res[i + j] = Math.min(res[i] + 1, res[i + j]); } else { break; } } } return res[arr.length - 1]; } }
总的来说。
坑一:对输入要处理,多行输入,判断终止输出是非逗号结尾,然后提取数字
坑二:输入的数据个数达到100000000。
坑三:尝试用过ArrayList来存储数据,依旧无效
#笔试题目##文远知行#