counterfeitdollar.java

来自「PKU中一些数据结构基本算法题的java实现」· Java 代码 · 共 88 行

JAVA
88
字号
package PKU;
import java.util.Scanner;


/**
 * ID:1013
 * 
 * @author yhm
 *
 */
public class CounterfeitDollar {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int caseNum = cin.nextInt();
		for(int i=0;i<caseNum;i++){
			String[] left = new String[3];
			String[] right = new String[3];
			String[] result = new String[3];
			for(int j=0;j<3;j++){
				left[j] = cin.next();
				right[j] = cin.next();
				result[j] = cin.next();
			}
			solve(left,right,result);
		}

	}

	static void solve(String[] left, String[] right, String[] result){
		for(char ch='A';ch<='L';ch++){
			if(isLight(ch,left,right,result)){
				System.out.println(ch+ " is the counterfeit coin and it is light. ");
				break;
			}
			else if(isHeavy(ch,left,right,result)){
				System.out.println(ch+ " is the counterfeit coin and it is heavy. ");
				break;
			}
		}
	}
	static boolean isLight(char x, String[] left, String[] right, String[] result){
		for(int i=0;i<3;i++){
			if(result[i].equals("up")){
				if(right[i].indexOf(x)<0){
					return false;
				}			
			}
			else if(result[i].equals("even")){
				if(left[i].indexOf(x)>=0 || right[i].indexOf(x)>=0){
					return false;
				}	
			}
			else{
				if(left[i].indexOf(x)<0){
					return false;
				}
			}
		}
		return true;
	}
	
	static boolean isHeavy(char x, String[] left, String[] right, String[] result){
		for(int i=0;i<3;i++){
			if(result[i].equals("up")){
				if(left[i].indexOf(x)<0){
					return false;
				}			
			}
			else if(result[i].equals("even")){
				if(left[i].indexOf(x)>=0 || right[i].indexOf(x)>=0){
					return false;
				}	
			}
			else{
				if(right[i].indexOf(x)<0){
					return false;
				}
			}
		}
		return true;
	}	
	
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?