1059. C语言竞赛 PAT JAVA里的一道题

C语言竞赛是浙江大学计算机学院主持的一个欢乐的竞赛。既然竞赛主旨是为了好玩,颁奖规则也就制定得很滑稽:

  1. 冠军将赢得一份“神秘大奖”(比如很巨大的一本学生研究论文集……)。
  2. 排名为素数的学生将赢得最好的奖品 —— 小黄人玩偶!
  3. 其他人将得到巧克力。

给定比赛的最终排名以及一系列参赛者的ID,你要给出这些参赛者应该获得的奖品。

输入格式:

输入第一行给出一个正整数N(<=10000),是参赛者人数。随后N行给出最终排名,每行按排名顺序给出一位参赛者的ID(4位数字组成)。接下来给出一个正整数K以及K个需要查询的ID。

输出格式:

对每个要查询的ID,在一行中输出“ID: 奖品”,其中奖品或者是“Mystery Award”(神秘大奖)、或者是“Minion”(小黄人)、或者是“Chocolate”(巧克力)。如果所查ID根本不在排名里,打印“Are you kidding?”(耍我呢?)。如果该ID已经查过了(即奖品已经领过了),打印“ID: Checked”(不能多吃多占)。

输入样例:
6
1111
6666
8888
1234
5555
0001
6
8888
0001
1111
2222
8888
2222
输出样例:
8888: Minion
0001: Chocolate
1111: Mystery Award
2222: Are you kidding?
8888: Checked
2222: Are you kidding?

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class B1059 {
    public static boolean isPrime(int num)
    {
        for (int n = 0; n < num; n++)
        {
            if (num % n != 0)
            {       
                return false;
            }
        }
        return true;        
    }

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int N = in.read();  
        int []awardID = new int[N];
        int i = 0;
        while (N > 0)  /*读取中奖名单*/
        {
            awardID[i++] = in.read();   
            N--;
        }

        int K = in.read();    
        int []checkID = new int[K];
        int []ifchecked = new int[K];
        for (int s = 0; s < K; s++)    /*统一设定未check*/
        {
            ifchecked[s] = 0;
        }
        int j = 0;

        while (j < K)
        {
            checkID[j++] = in.read();  /*输入查询序列*/
            if (ifchecked[j] == 0)
            { 
                for (int x = 0; x < N; x++)
                { 
                    ifchecked[j] = 1;     /*check过了*/
                    if (checkID[j] == awardID[x]) 
                    {
                        if (x == 1)
                        {
                            ifchecked[j] = 2; /*check过了且中奖了*/
                            System.out.print(checkID[j]+ ": " + "Mystery Award");
                        }
                        else if (isPrime(x))
                        {
                            ifchecked[j] = 2;
                            System.out.print(checkID[j]+ ": " + "Minion");                                  
                        }
                        else
                        {
                            ifchecked[j] = 2;
                            System.out.print(checkID[j]+ ": " + "Chocolate");
                        }                       
                    }                   
                }
                if (ifchecked[j] != 2)
                {
                    System.out.print(checkID[j]+ ": " + "Are you kidding?");                                                                        
                }               
            }           
            else
            {
                System.out.print(checkID[j]+ ": " + "Checked");     
            }                           
        }       
        in.close();
    }
}

0:Are you kidding?0:Are you kidding?0:Are you kidding?0:Are you kidding?0:Are you kidding?0:Are you kidding?0:Are you kidding?0:Are you kidding?
0:Are you kidding?

http://blog.csdn.net/jason95zhang/article/details/54644939

我自己写的:

    public static void main(String[] args) throws Exception {
        Scanner scanner = new Scanner(System.in);

        int a = Integer.parseInt(scanner.nextLine());
        //存放数字以及排名
        Map<String, Integer> map = new HashMap<>();
        for (int i = 0; i < a; i++) {
            map.put(scanner.nextLine(), i + 1);
        }

        int check = Integer.parseInt(scanner.nextLine());
        String[] checks = new String[check];
        for (int i = 0; i < check; i++) {
            checks[i] = scanner.nextLine();
        }

        for (int i = 0; i < check; i++) {

            String c = checks[i];

            if (!map.containsKey(c)) {
                System.out.println(c + " : Are you kidding?");
                continue;
            }

            int result = map.get(c);
            //如果数字变为两位表示查过了
            if (String.valueOf(result).length() == 2) {
                System.out.println(c + " : Checked");
                continue;
            }

            if (result == 1) {
                System.out.println(c + " : Mystery Award");
                continue;
            }

            if (isPrime(result)) {
                System.out.println(c + " : Minion");
            } else {
                System.out.println(c + " : Chocolate");
            }

            //查过了就把数字变为两位数
            map.put(c, result * 10 + 1);
        }
        scanner.close();
    }

    public static boolean isPrime(int num) {

        if (num < 2) {
            return false;
        }

        for (int i = 2; i <= Math.sqrt(num); i++) {
            if (num % i == 0) {
                return false;
            }
        }

        return true;

    }