poj1013

作者: Sophie12138 | 来源:发表于2017-02-24 23:08 被阅读54次

    题目描述:12个金币中有一个是假币,假币的大小颜色都和真的一样,只有重量不一样,但是有可能比真的轻,也有可能重,现在要通过称重找出哪个假的。
    输入格式:

    1
    ABCD EFGH even
    ABCI EFJK up
    ABIJ EFGH even
    

    说明:第三个字符串有三种情况”even”,”up”,”down”.分别表示平衡,左边轻,右边轻。

    输出格式:

    K is the counterfeit coin and it is light.
    

    思路:先用穷举将所有的表达式过滤,两边都相等的肯定是真币就是true,不为true的就判断被怀疑的次数,被怀疑的次数最多的,就是假币。

    java实现代码:

    
    import java.util.Scanner;
    
    public class TestForMySon {
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            if (scan.hasNext()) {
                int n = Integer.parseInt(scan.nextLine().trim());
                for (int i = 0; i < n; i++) {
                    boolean[] real = {false, false, false, false, false, false, false, false, false, false, false, false};
                    int[] hy = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
                    String[] s1 = scan.nextLine().trim().split(" ");
                    String[] s2 = scan.nextLine().trim().split(" ");
                    String[] s3 = scan.nextLine().trim().split(" ");
                    char[] c11 = s1[0].toCharArray();
                    char[] c12 = s1[1].toCharArray();
                    char[] c21 = s2[0].toCharArray();
                    char[] c22 = s2[1].toCharArray();
                    char[] c31 = s3[0].toCharArray();
                    char[] c32 = s3[1].toCharArray();
                    if (s1[2].equals("even")) {
                        for (int j = 0; j < c11.length; j++) {
                            real[c11[j] - 'A'] = true;
                        }
                        for (int j = 0; j < c12.length; j++) {
                            real[c12[j] - 'A'] = true;
                        }
                    }
                    if (s2[2].equals("even")) {
                        for (int j = 0; j < c21.length; j++) {
                            real[c21[j] - 'A'] = true;
                        }
                        for (int j = 0; j < c22.length; j++) {
                            real[c22[j] - 'A'] = true;
                        }
                    }
                    if (s3[2].equals("even")) {
                        for (int j = 0; j < c31.length; j++) {
                            real[c31[j] - 'A'] = true;
                        }
                        for (int j = 0; j < c32.length; j++) {
                            real[c32[j] - 'A'] = true;
                        }
                    }
                    if (s1[2].equals("up")) {
                        for (int j = 0; j < c11.length; j++) {
                            hy[c11[j] - 'A']++;
                        }
                        for (int j = 0; j < c12.length; j++) {
                            hy[c12[j] - 'A']--;
                        }
                    }
                    if (s2[2].equals("up")) {
                        for (int j = 0; j < c21.length; j++) {
                            hy[c21[j] - 'A']++;
                        }
                        for (int j = 0; j < c22.length; j++) {
                            hy[c22[j] - 'A']--;
                        }
                    }
                    if (s3[2].equals("up")) {
                        for (int j = 0; j < c31.length; j++) {
                            hy[c31[j] - 'A']++;
                        }
                        for (int j = 0; j < c32.length; j++) {
                            hy[c32[j] - 'A']--;
                        }
                    }
                    if (s1[2].equals("down")) {
                        for (int j = 0; j < c11.length; j++) {
                            hy[c11[j] - 'A']--;
                        }
                        for (int j = 0; j < c12.length; j++) {
                            hy[c12[j] - 'A']++;
                        }
                    }
                    if (s2[2].equals("down")) {
                        for (int j = 0; j < c21.length; j++) {
                            hy[c21[j] - 'A']--;
                        }
                        for (int j = 0; j < c22.length; j++) {
                            hy[c22[j] - 'A']++;
                        }
                    }
                    if (s3[2].equals("down")) {
                        for (int j = 0; j < c31.length; j++) {
                            hy[c31[j] - 'A']--;
                        }
                        for (int j = 0; j < c32.length; j++) {
                            hy[c32[j] - 'A']++;
                        }
                    }
                    int m = 0;
                    for (int k = 0; k < real.length; k++) {
                        if (!real[k]) {
                            int max = Math.abs(hy[k]);
                            m = k;
                            for (int j = k + 1; j < hy.length; j++) {
                                if (Math.abs(hy[j]) > max && !real[j]) {
                                    max = Math.abs(hy[j]);
                                    m = j;
                                }
                            }
                            break;
                        }
                    }
                    char key = (char) ('A' + m);
                    if (hy[m] > 0) {
                        System.out.println(key + " is the counterfeit coin and it is heavy.");
                    } else {
                        System.out.println(key + " is the counterfeit coin and it is light.");
                    }
                }
            }
        }
    }
    
    

    相关文章

      网友评论

          本文标题:poj1013

          本文链接:https://www.haomeiwen.com/subject/blkgwttx.html