PAT.1059. C Language Competition

topic

Time limit :
200 ms ;
Memory limit : 65536
kB ;
Code length :
8000 ; Since the theme of the competition is for fun, the award rules are also very funny:




0. The champion will win a "mystery prize" (such as a huge collection of student research papers...).
1. Students ranked prime will win the best prize - Minion dolls!
2. Others will get chocolate.

Given the final standings of the competition and a list of entrant IDs, you give the prizes that those entrants should receive.
Input format:
The first line of input gives a positive integer N (<=10000), which is the number of contestants. Then N lines give the final ranking, and each line gives the ID (4 digits) of a contestant in ranking order. Next, a positive integer K and K IDs to be queried are given.
Output format:
For each ID to be queried, output "ID: Prize" in one line, where the prize is either "Mystery Award", or "Minion", or "Chocolate" (chocolate). If the searched ID is not in the ranking at all, print "Are you kidding?" If the ID has been checked (that is, the prize has been received), print "ID: Checked" (you cannot eat more and take more).
Input sample:

6
1111
6666
8888
1234
5555
0001
6
8888
0001
1111
2222
8888
2222

Sample output:

8888: Minion
0001: Chocolate
1111: Mystery Award
2222: Are you kidding?
8888: Checked
2222: Are you kidding?

analyze:

At first glance, it looks like bucket sort, just put the mark in it, but a careful analysis is actually more complicated. We implement it with a mark array.
Five situations need to be distinguished:
1. Champion
2. Prime number ranking
3. Other rankings
4. No ID
5. ID checked
First, consider that the ID has been checked and the ID does not exist. We use -1 for the former and -1 for the latter. Set to 0 to distinguish.
The second is the champion and other rankings. When we consider marking the champion, mark it as "big",
so sort it out:
1. Consider whether the mark is 0
2. If it is not 0, whether it is -1
3. If it is not -1, whether to mark "big" , whether it is a prime number ranking, and finally the other ranking

Code (cpp):

#include<iostream>
using namespace std;
bool judge(int x){
    for(int i=2;i*i<=x;i++)
        if(x%i==0)
            return false;
    return true;
}
int main(){
    int n,k;
    int mark[10000+5]={0};
    cin>>n;
    for(int i=0;i<n;i++){
        int t;
        cin>>t;
        if(i==0)
            mark[t]=i+1+10000;
        else
            mark[t]=i+1;
    }
    cin>>k;
    for(int i=0;i<k;i++){
        int t;
        cin>>t;
        if(mark[t]!=0){
            if(mark[t]==-1)
                printf("%04d: Checked\n",t);
            else{
                if(mark[t]>10000)
                    printf("%04d: Mystery Award\n",t);
                else if(judge(mark[t]))
                    printf("%04d: Minion\n",t);
                else
                    printf("%04d: Chocolate\n",t);
                mark[t]=-1;
            }
        }else{
            printf("%04d: Are you kidding?\n",t);
        }
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324724978&siteId=291194637