poj1013

题目描述: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.");
                }
            }
        }
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,200评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,823评论 18 399
  • 喜宝动力 专注电商运营营销服务,近年来业务快速增长,毛利率有下滑,主要是SAAS平台收入下降,整合营销及dsp收入...
    彼得新阅读 145评论 0 0
  • “哟!这是谁家的小孩啊!白白胖胖,以后肯定是当官的料啊!” “小朋友,你多大了啊?上几年级了?——噢,好好学习,将...
    诗意Prince阅读 333评论 0 1
  • 硕沙粒阅读 132评论 0 1