请求各位大佬教教我最后两题
第一题:
package meituan; import java.util.Scanner; /** * @author Rorke * @date 2020/8/22 16:06 */ public class Q1 { private static final String ERR_RESULT = "Wrong"; private static final String SUC_RESULT = "Accept"; public static void main(String[] args) { Scanner input = new Scanner(System.in); String help = input.nextLine(); int n = Integer.parseInt(help); while (n > 0) { String username = input.nextLine(); if (isStartWithLetter(username)) { if (isValidUsername(username)) { System.out.println(SUC_RESULT); } else { System.out.println(ERR_RESULT); } } else { System.out.println(ERR_RESULT); } n--; } } private static boolean isStartWithLetter(String username) { if (username.isEmpty()) { return false; } char c = username.charAt(0); return isLetter(c); } private static boolean isValidUsername(String username){ boolean numFlag = false; for (int i=1;i<username.length();i++){ char c = username.charAt(i); if(isLetter(c)){ continue; } if(isNumber(c)){ numFlag = true; } if(!isLetter(c)&&!isNumber(c)){ return false; } } return numFlag; } private static boolean isNumber(char c){ return c>='0'&&c<='9'; } private static boolean isLetter(char c){ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); } }第二题:
package meituan; import java.util.*; /** * @author Rorke * @date 2020/8/22 16:23 */ public class Q2 { static class Order { int v, w, orderNum, total; public Order(int v, int w, int orderNum) { this.v = v; this.w = w; this.orderNum = orderNum; this.total = v + 2 * w; } } public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(), m = input.nextInt(); Queue<Order> queue = new PriorityQueue<>((o1, o2) -> o1.total == o2.total ? o1.orderNum - o2.orderNum : o2.total - o1.total); for (int i = 0; i < n; i++) { int v = input.nextInt(), w = input.nextInt(); queue.offer(new Order(v,w,i+1)); } List<Integer> result = new ArrayList<>(); for (int i = 0; i < m; i++) { result.add(queue.remove().orderNum); } result.sort(Comparator.comparingInt(i -> i)); result.forEach(i-> System.out.print(i+" ")); } }第三题:
package meituan; import java.util.Scanner; /** * @author Rorke * @date 2020/8/22 16:45 */ public class Q3 { private static final int STOP_SIGNAL = 0; public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); int[] weight = new int[n]; int[] sum = new int[n]; for (int i = 0; i < n; i++) { weight[i] = input.nextInt(); sum[i] = weight[i]; if(i!=0){ sum[i] += sum[i-1]; } } int[] orders = new int[n]; for (int i = 0; i < n; i++) { orders[i] = input.nextInt()-1; } for (int order: orders) { int i = order+1; while(i<n&&sum[i]!=STOP_SIGNAL){ sum[i] -= sum[order]; i++; } sum[order] = STOP_SIGNAL; int maxValue = Integer.MIN_VALUE; for (int s : sum) { maxValue = Math.max(s,maxValue); } System.out.println(maxValue); } } }
全部评论
(3) 回帖