荣耀8.20笔试
第一题:思路:简单模拟即可
public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); in.nextLine(); String str = in.nextLine(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i += 9) { StringBuilder temp = new StringBuilder(str.substring(i, i + 9)); if (str.charAt(i) == 0) { sb.append(temp); } else { sb.append(temp.reverse()); } sb.append(" "); } }第二题:思路:正则匹配+优先级队列排序
public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.nextLine(); TreeSet<Pair> ts = new TreeSet<>((a, b) -> { if (a.timestamp.equals(b.timestamp)) { if (a.str.length() == b.str.length()) { return a.str.compareTo(b.str); } else { return a.str.length() - b.str.length(); } } else { return a.timestamp.compareTo(b.timestamp); } }); for (int i = 0; i < n; i++) { String str = sc.nextLine(); String regex = "[0-9]{4}[-][0-9]{2}[-][0-9]{2}[T][0-9]{2}[:][0-9]{2}[:][0-9]{2}"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(str); String timestamp = null; if (m.find()) { timestamp = m.group(); } ts.add(new Pair(str, timestamp)); } for (Pair p : ts) { System.out.println(p.str); } } static class Pair { String str; String timestamp; Pair(String str, String timestamp) { this.str = str; this.timestamp = timestamp; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair pair = (Pair) o; return pair.str.equals(str) && pair.timestamp.equals(timestamp); } @Override public int hashCode() { return Objects.hash(str, timestamp); } }第三题:暴力回溯只过了一半,思路:对于每一个数,遍历队伍的每一行看能不能放,最后新建一行并且加入。
static List<List<Integer>> res = new ArrayList<>(); public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.nextLine(); String[] strs = s.split(", "); int[] nums = new int[strs.length]; for (int i = 0; i < strs.length; i++) { nums[i] = Integer.parseInt(strs[i]); } Arrays.sort(nums); List<List<Integer>> rows = new ArrayList<>(); backTrack(rows, nums, 0); System.out.println("lenth:" + nums.length / res.size()); for (List<Integer> r : res) { System.out.println(r); } } static void backTrack(List<List<Integer>> rows, int[] nums, int index) { if (index == nums.length) { if (res.size() == 0 ||( rows.size() < res.size())) { res = new ArrayList<>(); if (check(rows)) { for (List<Integer> row : rows) { res.add(new ArrayList<>(row)); } } } return; } int num = nums[index]; for (int i = 0; i < rows.size(); i++) { List<Integer> row = rows.get(i); if (row.size() > 1) { int last = row.get(row.size() - 1); int preLast = row.get(row.size() - 2); if (num - last != last - preLast) { continue; } } if (row.get(0) == num) continue; row.add(num); backTrack(rows, nums, index + 1); row.remove(row.size() - 1); } rows.add(new ArrayList<>()); rows.get(rows.size() - 1).add(num); backTrack(rows, nums, index + 1); rows.remove(rows.size() - 1); } static boolean check(List<List<Integer>> rows) { int n = rows.get(0).size(); for (int i = 1; i < rows.size(); i++) { if (rows.get(i).size() != n) return false; } return true; }