一、题目
C 语言竞赛是浙江大学计算机学院主持的一个欢乐的竞赛。既然竞赛主旨是为了好玩,颁奖规则也就制定得很滑稽:
- 0、冠军将赢得一份“神秘大奖”(比如很巨大的一本学生研究论文集……)。
- 1、排名为素数的学生将赢得最好的奖品 —— 小黄人玩偶!
- 2、其他人将得到巧克力。
给定比赛的最终排名以及一系列参赛者的 ID,你要给出这些参赛者应该获得的奖品。
输入格式:
输入第一行给出一个正整数 N ( ≤ 1 0 4 ) N(≤10^{4}) N(≤104)是参赛者人数。随后 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?
二、思路和注意事项
- 不能用printf("%s") 去输出一个string类的结构体变量,会出现错误:
[Error] cannot pass objects of non-trivially-copyable type 'std::string {aka class std::basic_string - 如果定义的参赛选手的编号是int类型的,输出的时候要printf("%04d",…),否则样例:0001会输出成1。
定义的判断是否是素数的函数如下:
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;
}
三、AC代码
#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;
}
结果:运行时间还是太长,提交的测试点2一会过一会不过。