华为OD机试-分班问题
import java.util.ArrayDeque; import java.util.Objects; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { /** * 注意,按照题目要求,本代码没有处理不合规字符,这点遍历的时候验证一下就行 */ String[] students = {"1/N", "2/Y", "3/N", "4/Y"}; ArrayDeque<String> class_a = new ArrayDeque<>(); // A班 ArrayDeque<String> class_b = new ArrayDeque<>(); // B班 ArrayDeque<String> class_pre = null; for (String student : students) { if (Objects.isNull(class_pre)) { class_pre = class_a; class_pre.offerLast(student); continue; } if (student.endsWith("N")) { class_pre = class_pre.equals(class_a) ? class_b : class_a; } class_pre.offerLast(student); } System.out.println(class_a.stream().map(s -> s.split("/")[0]).collect(Collectors.joining(" ")).trim()); System.out.println(class_b.stream().map(s -> s.split("/")[0]).collect(Collectors.joining(" ")).trim()); } }