2692:假币问题 百练noi 枚举解答

http://bailian.openjudge.cn/practice/2692

import java.util.Scanner;

public class Main{
    static int status[] = new int[12];
    static String left[] = new String[3];
    static String right[] = new String[3];
    static String result[] = new String[3];
    static boolean Balanced(){
        int i,k,leftW,rightW;
        for ( i = 0; i < 3; i++) {
            leftW = rightW = 0;
            int len = left[i].length();
            for (k = 0; k < len; k++) {
                leftW += status[left[i].charAt(k) - 'A'];
                rightW += status[right[i].charAt(k) - 'A'];
            }
            if (leftW > rightW && result[i].charAt(0) != 'u'){
                return false;
            }
            if (leftW == rightW && result[i].charAt(0) != 'e'){
                return false;
            }
            if (leftW < rightW && result[i].charAt(0) != 'd'){
                return false;
            }
        }
        return  true;
    }
    public static void main(String[] args) {
        int i,num;
        Scanner sc = new Scanner(System.in);
        num= sc.nextInt();
        while (num>0){
            num--;
            for (i = 0;  i<3 ; i++) {
                String lleft = sc.next();
                String rright = sc.next();
                String rresult = sc.next();
                    left[i] = lleft;
                    right[i] = rright;
                    result[i] = rresult;
            }
            for ( i = 0; i < 12; i++) {
                status[i] = 0;
            }
            for ( i = 0; i < 12; i++) {
                status[i] = 1;
                if (Balanced()) {
                    break;
                }
                status[i] = -1;
                if (Balanced()) {
                    break;
                }
                status[i] = 0;
            }
            String result =status[i]>0 ? "heavy":"light";
            System.out.println((char) (i+'A') +" is the counterfeit coin and it is "+result+".");
        }
    }
}
posted @ 2021-04-05 20:43  符瑾瑜  阅读(294)  评论(0)    收藏  举报