博客
关于我
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/

    你可能感兴趣的文章
    NN&DL4.1 Deep L-layer neural network简介
    查看>>
    NN&DL4.3 Getting your matrix dimensions right
    查看>>
    NN&DL4.8 What does this have to do with the brain?
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>
    No resource identifier found for attribute 'srcCompat' in package的解决办法
    查看>>
    No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
    查看>>
    NO.23 ZenTaoPHP目录结构
    查看>>
    NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
    查看>>
    NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
    查看>>
    Node JS: < 一> 初识Node JS
    查看>>
    Node-RED中使用JSON数据建立web网站
    查看>>