11.15笔试 第一题65%
路过的朋友们帮看看哪里有问题呢,测试案例都可以通过
import java.util.Scanner; /** 测试案例 * 5 * 01.a.111111.2.22222.31.3.rc11.10.0b2222210.1.c11111 * * 5 * 1.2.02555 1.2.2555 1.2.ABA 1.2.0ABA 1.2.0ABA.C */ public class Main { public static void main(String[] args) { //读取输入并转换成字符串数组 Scanner s = new Scanner(System.in); int n = s.nextInt(); s.nextLine(); String[] inputStrings = s.nextLine().split(" "); StringBuilder result = new StringBuilder(); //用来标记是否前后相邻且 重复 string.equals() String cur = ""; //对每个字符串进行判断是否 合法的版本号 for (String word : inputStrings) { //判断前3个条件 String afterRemoveHead0 = checkFisrtThreeRules(word); if (afterRemoveHead0 != null) { //第4个条件判断 是否和前一个重复 if (!cur.equals(afterRemoveHead0)) { result.append(afterRemoveHead0 + " "); cur = afterRemoveHead0; } } } result.deleteCharAt(result.length() - 1); System.out.print(result.toString()); } static String checkFisrtThreeRules(String s) { StringBuilder stringBuilder = new StringBuilder(); //第一条规则,首先以英文点分割三段 String[] split = s.split("\\."); if (split.length != 3) { return null; } //第2条规则,去除第三段包含 rc if (split[2].contains("rc")) { return null; } //第3条规则,去除有 英文字符的 if (split[0].matches(".*[a-zA-Z].*") || split[1].matches(".*[a-zA-Z].*")) { return null; } //第5个条件 去除 前导0 for (String parts : split) { stringBuilder.append(parts.replaceFirst("^0+", "")); stringBuilder.append('.'); } stringBuilder.deleteCharAt(stringBuilder.length() - 1); return stringBuilder.toString(); } }#悬赏#