Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
JAVA 递归做的 AC了 注意逻辑运算符 && 如果左边值为false 右边就不执行了import java.util.Iterator; import java.util.Scanner; import java.util.Vector; public class Main { private int p, q, r, s, t; private String inputString; private char[] inputStringToArray; private Scanner sc; private int storey;//递归层数 public Main() { sc = new Scanner(System.in); storey = 0; } public String tautologyOrNot() { inputString(); if (inputString.equals("0")){ return "SystemExit"; } boolean jumpOut = true; for (p = 0; p <= 1 && jumpOut; p ++) for (q = 0; q <= 1 && jumpOut; q ++) for (r = 0; r <= 1 && jumpOut; r ++) for (s = 0; s <= 1 && jumpOut; s ++) for (t = 0; t <= 1 && jumpOut; t ++) { storey = 0; if (!judgement()){ jumpOut = false; } } if (jumpOut) return "tautology"; else return "not"; } private void inputString() { inputString = sc.next(); inputStringToArray = inputString.toCharArray(); } private boolean judgement() { char symbol = inputStringToArray[storey ++]; switch(symbol) { case 'K':{ boolean w = judgement(); boolean x = judgement(); if(w && x) return true; else return false; } case 'A' :{ boolean w = judgement(); boolean x = judgement(); if(w || x) return true; else return false; } case 'N' :{ boolean w = judgement(); if(!w) return true; else return false; } case 'C' :{ boolean w = judgement(); boolean x = judgement(); if( (!w) || x ) return true; else return false; } case 'E' :{ boolean w = judgement(); boolean x = judgement(); if(w == x) return true; else return false; } case 'p' : { if (p == 1) return true; else return false; } case 'q' : { if (q == 1) return true; else return false; } case 'r' : { if (r ==1) return true; else return false; } case 's' : { if (s == 1) return true; else return false; } case 't' : { if (t == 1) return true; else return false; } default :return true; } } public static void main(String[] args) { Main tautology = new Main(); Vector<String> result = new Vector<String>(); String value; while(true) { value = tautology.tautologyOrNot(); if(value.equals("SystemExit")) break; else result.add(value); } Iterator<String> iter; iter = result.iterator(); while(iter.hasNext()){ System.out.println(iter.next()); } } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator