C 语言竞赛是浙江大学计算机学院主持的一个欢乐的竞赛。既然竞赛主旨是为了好玩,颁奖规则也就制定得很滑稽:
- 0、冠军将赢得一份“神秘大奖”(比如很巨大的一本学生研究论文集……)。
- 1、排名为素数的学生将赢得最好的奖品 —— 小黄人玩偶!
- 2、其他人将得到巧克力。
给定比赛的最终排名以及一系列参赛者的 ID,你要给出这些参赛者应该获得的奖品。
输入格式:
输入第一行给出一个正整数 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?
解题思路:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#define MAXN 64
bool isPrime(int n);
int compare(const void *a, const void *b);
typedef struct _student {
int id;
bool check;
char award[MAXN];
} Stu;
typedef const struct _student *PtrS;
int main(int argc, const char *argv[])
{
int N, K, i;
if ( scanf("%d", &N)==EOF ) printf("error\n");
Stu student[N];
if ( scanf("%d", &student[0].id)==EOF ) printf("error\n");
student[0].check = false;
strcpy(student[0].award, "Mystery Award");
for ( i=1; i<N; ++i ) {
if ( scanf("%d", &student[i].id)==EOF ) printf("error\n");
student[i].check = false;
if ( isPrime(i+1) )
strcpy(student[i].award, "Minion");
else
strcpy(student[i].award, "Chocolate");
}
qsort(student, N, sizeof(student[0]), compare);
if ( scanf("%d", &K)==EOF ) printf("error\n");
int checkID;
Stu *pc;
while ( K-- ) {
if ( scanf("%d", &checkID)==EOF ) printf("error\n");
pc = (Stu*)bsearch(&checkID, student, N, sizeof(student[0]), compare);
if ( pc ) {
if ( pc->check ) {
printf("%04d: Checked\n", pc->id);
} else {
pc->check = true;
printf("%04d: %s\n", pc->id, pc->award);
}
} else {
printf("%04d: Are you kidding?\n", checkID);
}
}
return EXIT_SUCCESS;
}
bool isPrime( int n )
{
if ( n < 2 ) {
return false;
} else if ( n == 2 ) {
return true;
} else {
int i;
for ( i=2; i*i<=n; ++i ) {
if ( n % i == 0 )
return false;
}
return true;
}
}
int compare(const void *a, const void *b)
{
PtrS pa = a;
PtrS pb = b;
return pa->id - pb->id;
}
4756

被折叠的 条评论
为什么被折叠?



