博客
关于我
PAT——1059. C语言竞赛
阅读量:463 次
发布时间:2019-03-06

本文共 2460 字,大约阅读时间需要 8 分钟。

为了解决这个问题,我们需要根据参赛者的排名和查询结果,确定他们应该获得的奖品。参赛者根据排名获得不同的奖品:冠军获得“神秘大奖”,排名为素数的获得“小黄人”,其他人获得巧克力。如果查询的ID不在排名中,则输出“耍我呢?”。如果ID已经被查过,则输出“不能多吃多占”。

方法思路

  • 读取输入:首先读取参赛人数N,然后读取N个参赛者的ID,接着读取查询人数K和K个查询的ID。
  • 存储参赛者ID:将参赛者的ID存储在一个数组中。
  • 处理查询:对于每个查询的ID,检查它是否存在于参赛者排名中。如果不存在,输出“耍我呢?”。如果存在,检查是否已经被处理过。如果是新处理的,根据排名判断奖品:
    • 第一个参赛者获得“神秘大奖”。
    • 排名为素数的参赛者获得“小黄人”。
    • 其他参赛者获得巧克力。
  • 素数判断:编写一个函数来判断一个数是否为素数。
  • 解决代码

    import java.util.Scanner;public class BasicalLevel1059CproCompition {    public static void main(String[] args) {        Scanner in = new Scanner(System.in);        int N = Integer.parseInt(in.nextLine());        String[] a = new String[N];        for (int i = 0; i < N; i++) {            a[i] = in.nextLine();        }        int K = Integer.parseInt(in.nextLine());        boolean[] processed = new boolean[N];        for (int i = 0; i < K; i++) {            String m = in.nextLine();            int index = -1;            boolean found = false;            for (int j = 0; j < N; j++) {                if (m.equals(a[j])) {                    found = true;                    index = j;                    break;                }            }            if (!found) {                System.out.println(m + ": Are you kidding?");            } else {                if (processed[index]) {                    System.out.println(m + ": Checked");                } else {                    if (index == 0) {                        System.out.println(m + ": Mystery Award");                    } else {                        int rank = index + 1;                        if (isPrime(rank)) {                            System.out.println(m + ": Minion");                        } else {                            System.out.println(m + ": Chocolate");                        }                    }                    processed[index] = true;                }            }        }    }    private static boolean isPrime(int i) {        if (i <= 1) {            return false;        }        if (i == 2) {            return true;        }        if (i % 2 == 0) {            return false;        }        for (int j = 3; j <= Math.sqrt(i); j += 2) {            if (i % j == 0) {                return false;            }        }        return true;    }}

    代码解释

  • 读取输入:使用Scanner读取输入数据,首先读取参赛人数N,然后读取N个参赛者的ID。
  • 存储参赛者ID:将参赛者的ID存储在数组a中。
  • 处理查询:对于每个查询的ID,检查它是否存在在数组中。如果不存在,输出相应的结果。如果存在,检查是否已经被处理过。如果是新处理的,根据排名判断奖品。
  • 素数判断isPrime函数用于判断一个数是否为素数,用于判断排名是否为素数以决定奖品类型。
  • 这个方法确保了每个查询的ID都能得到正确的奖品判断,并且处理了重复查询和不存在的ID情况。

    转载地址:http://dnnbz.baihongyu.com/

    你可能感兴趣的文章
    npm install 报错 no such file or directory 的解决方法
    查看>>
    npm install 权限问题
    查看>>
    npm install报错,证书验证失败unable to get local issuer certificate
    查看>>
    npm install无法生成node_modules的解决方法
    查看>>
    npm install的--save和--save-dev使用说明
    查看>>
    npm node pm2相关问题
    查看>>
    npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
    查看>>
    npm run build报Cannot find module错误的解决方法
    查看>>
    npm run build部署到云服务器中的Nginx(图文配置)
    查看>>
    npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
    查看>>
    npm scripts 使用指南
    查看>>
    npm should be run outside of the node repl, in your normal shell
    查看>>
    npm start运行了什么
    查看>>
    npm WARN deprecated core-js@2.6.12 core-js@<3.3 is no longer maintained and not recommended for usa
    查看>>
    npm 下载依赖慢的解决方案(亲测有效)
    查看>>
    npm 安装依赖过程中报错:Error: Can‘t find Python executable “python“, you can set the PYTHON env variable
    查看>>
    npm.taobao.org 淘宝 npm 镜像证书过期?这样解决!
    查看>>
    npm—小记
    查看>>
    npm介绍以及常用命令
    查看>>
    NPM使用前设置和升级
    查看>>