//提供下java版本的代码 public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextLine()) { // 注意 while 处理多个 case String str = in.nextLine(); String s = str.replace(' ','\n'); boolean flag = false; StringBuilder res = new StringBuilder(); int cnt = 0;//用于统计出现的\n数量 //把双引号中的\n替换成空格 for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == '"') flag = !flag; //拨动开关进入下一轮循环,顺带达到去除引号的作用 else if (flag && s.charAt(i) == '\n' ) { res.append(' '); cnt--; }else { res.append(s.charAt(i)); } if (s.charAt(i) == '\n') cnt++; } System.out.println(cnt+1); System.out.println(res.toString()); } }
点赞

相关推荐

牛客网
牛客网在线编程
牛客网题解
牛客企业服务