PAT-B: 1059 C language competition (20 points)



1. Topic

The C language contest is a joyous contest hosted by the School of Computer Science, Zhejiang University. Since the theme of the competition is for fun, the awarding rules are also very funny:

  • 0. The champion will win a "mystery award" (such as a huge collection of student research papers...).
  • 1. The student with a prime number will win the best prize-the little yellow doll!
  • 2. Others will get chocolate.

Given the final ranking of the competition and a series of participant IDs, you have to give the prizes that these participants should receive.

Input format:
Input the first line to give a positive integer N (≤ 1 0 4) N (≤10^{4})N104 )is the number of participants. The next N rows give the final ranking, and each row gives a contestant's ID (4 digits) in the order of ranking. Next, a positive integer K and K IDs to be queried are given.

Output format:
For each ID to be queried, output ID: prizes in one line. The prizes are either Mystery Award, Minion, or Chocolate. If the ID you checked is not in the ranking at all, print Are you kidding?. If the ID has been checked (that is, the prize has already been received), print the ID: Checked (you can't eat more and take up 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?


2. Ideas and precautions

  1. You cannot use printf("%s") to output a string class structure variable, an error will occur:
    [Error] cannot pass objects of non-trivially-copyable type'std::string {aka class std::basic_string
  2. If the number of the defined contestant is of type int, printf("%04d",...) is required when outputting , otherwise the sample: 0001 will be output as 1.

The defined function to determine whether it is a prime number is as follows:

bool prime(int p)
{
    
    
	if(p<0||p==1||p==0) 
	{
    
    
		return 0;
	}
	for(int i=2;i<=sqrt(p);i++)
	{
    
    
		if(p%i==0)return 0;
	} 
	return 1;
} 



Three, AC code

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
bool prime(int n); 

struct participant
{
    
    
		int id;
		int ranking; 	//名次 
		string ans;		//对应的语句
		bool prim=false;
		int flag=0;		//判断领奖次数 
			
}pt[10001];


int main()
{
    
    
	int N,K;
	cin>>N; 
	for(int i=1;i<=N;i++)
	{
    
    
		pt[i].ranking=i;		//赋予名次 
		cin>>pt[i].id;			//参赛编号
		if(prime(i))
		{
    
    
			pt[i].ans="Minion";
		}
		else if(i==1)
		{
    
    
			pt[i].ans="Mystery Award";
		}
		else 
		{
    
    
			pt[i].ans="Chocolate";
		}
	}
	
	cin>>K;
	for(int i=1;i<=K;i++)
	{
    
    
			int temp_id;
			cin>>temp_id;
			int flag_id=0;
			int temp_j;
		for(int j=1;j<=N;j++)
		{
    
    
			temp_j=j;
			if(pt[j].id==temp_id)
			{
    
    
				    flag_id=1;
					if(pt[j].flag==0)
				{
    
    
					printf("%04d: ",pt[j].id); 
					cout<<pt[j].ans<<endl;
					pt[j].flag=1;
				}
				else 
				{
    
    		
					printf("%04d: ",pt[j].id); 
					cout<<"Checked"<<endl;
				}
			}
		}
		if(flag_id==0)
		{
    
    
			printf("%04d: ",temp_id); 
			cout<<"Are you kidding?"<<endl;
		}
	}		
}


bool prime(int p)
{
    
    
	if(p<0||p==1||p==0) 
	{
    
    
		return 0;
	}
	
	for(int i=2;i<=sqrt(p);i++)
	{
    
    
		if(p%i==0)return 0;
	} 
	return 1;
} 

Result: The running time is still too long, and the submitted test point 2 will not pass after a while.
Insert picture description here

Guess you like

Origin blog.csdn.net/SKMIT/article/details/114136491