贴上菜鸡Java第一题代码,看到多行数据就改了下循环,脑子抽了没把代码里的 return 去掉,结束了才看见。。怪不得读不到多行的数据(awsl)。只过了50%,但我觉得应该能ac,欢迎指错讨论。 import java.util.*; public class Main {     public static void main(String[] args) {         Scanner sc = new Scanner(System.in);         while(sc.hasNext()) {             String[] arr = sc.nextLine().split("\\s+");             // 首先把 error 的情况搞出来             // and or 不能开头,不能结尾             // true false 类别不能连续             // and or 类别也不能连续             boolean preIsAndOr = true;             boolean error = false;             for(int i = 0; i < arr.length; i ++) {                 String s = arr[i];                 if(preIsAndOr && ("and".equals(s) || "or".equals(s))) {                     error = true;                 } else if(!preIsAndOr && ("true".equals(s) || "false".equals(s))) {                     error = true;                 }                 preIsAndOr = "and".equals(s) || "or".equals(s) ? true : false;             }                          if(error || preIsAndOr) {                 System.out.println("error");                 continue;             }             // 能到这,说明序列本身已经没有问题了。             // 碰到 and 直接算             LinkedList<String> stack = new LinkedList<>();             for(int i = 0; i < arr.length; i ++) {                 String s = arr[i];                 if("true".equals(s) || "false".equals(s)) {                     stack.push(s);                 } else if("and".equals(s)) {                     String pre = stack.pop();                     String next = arr[++ i];                     if("false".equals(next) || "false".equals(pre)) {                         stack.push("false");                     } else {                         stack.push("true");                     }                 }             }             // 栈里的都当 or 处理             String res = "false";             for(String s : stack) {                 if("true".equals(s)) {                     res = "true";                     break;                 }             }             System.out.println(res);         }              }      }
点赞 5

相关推荐

牛客网
牛客企业服务