9.9携程后台笔试AK代码
三个编程题1. linux命令2. 一组数划分为k个区间3. 01字符串 消去1public class test10 { //第一题 public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.nextLine()); String[] ops = new String[n]; for(int i=0; i < n; i++){ ops[i] = sc.nextLine(); } StringBuilder sb = new StringBuilder(); for(int i=0; i < n; i++){ if(ops[i].startsWith("cd")){ String s = ops[i].substring(3); if(s.equals("..")){ int le = sb.length(); while(le > 0 && sb.charAt(le-1) != '\\'){ sb.deleteCharAt(le-1); le--; } if(le >= 1) sb.deleteCharAt(le-1); } else sb.append('\\').append(s); } else{ if(sb.length() == 0) System.out.println('\\'); else System.out.println(sb.toString()); } } } //***第二题 public static void main2(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); int[] nums = new int[n]; for(int i=0; i < n; i++) nums[i] = sc.nextInt(); int max = nums[0], min = nums[0]; for(int num : nums){ max = Math.max(max, num); min = Math.min(min, num); } int l = 0, r = max - min, m = 0; while(l < r){ m = (l + r) / 2; if(check(nums, k, m)) r = m; else l = m + 1; } System.out.println(l); } private static boolean check(int[] nums, int k, int x){ int max = nums[0], min = nums[0]; for(int i=1; i < nums.length; i++){ max = Math.max(max, nums[i]); min = Math.min(min, nums[i]); if(max - min > x){ k--; if(k <= 0) return false; max = min = nums[i]; } } return k > 0; } //***第三题 public static void main3(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); String str = sc.next(); int[][] score = new int[m][2]; for(int i=0; i < m; i++){ score[i][0] = sc.nextInt(); score[i][1] = sc.nextInt(); } //这里用Map不行,因为Map无序 Arrays.sort(score, (a, b)->{ return a[0] - b[0]; }); List<Integer> list = new ArrayList<>(); int cnt = 0, max = 0; for(char ch : str.toCharArray()){ if(ch == '1') cnt++; else{ max = Math.max(max, cnt); if(cnt != 0) list.add(cnt); cnt = 0; } } max = Math.max(max, cnt); if(cnt != 0) list.add(cnt); int[] dp = new int[max+1]; for(int i=1; i <= max; i++){ for(int j=0; j < m && score[j][0] <= i; j++){ int x = score[j][0], y = score[j][1]; for(int t=1; t * x <= i; t++){ dp[i] = Math.max(dp[i], dp[i - t * x] + t * y); } } } int ans = 0; for(int num : list){ ans += dp[num]; } System.out.println(ans); } }
#笔经#