乙级的题目训练主要用来熟悉编程语言的语法和形成良好的编码习惯和编码规范。从小白开始逐步掌握用编程解决问题。
PAT乙级BasicLevelPractice 1059
问题分析
题目要求按照参赛选手名次和颁奖规则为选手颁奖。 由于是题目给定了明确的规则, 所以仍然是模拟题的思路。
完整描述步骤
- 获取输入: 参数选手信息(ID, 输入次序为排名)
- 对于每一个参赛选手:
- 如果名次是1, 则送神秘大奖;
- 如果名次是质数, 则送小黄人玩偶;
- 否则送巧克力;
- 获取输入: 查询次数
- 如果查询的ID存在, 且是第一次查询该ID, 则输出奖品
- 如果查询的ID存在, 但不是第一次查询该ID, 则输出Checked
- 如果查询的ID不存在, 则输出Are you kidding?
伪代码描述
- get input: player_amount
- init recorder:
- get input:
- player_ID (as index in player_info)
- player_rank
- is input order
- stored as value of player_info[player_ID])
- for each player:
- if rank == 1:
- rewards[player.ID] = ‘Mystery Award’;
- else if is_prime(rank):
- rewards[player.ID] = ‘Minion’;
- else:
- rewards[player.ID] = ‘Chocolate’;
- get input: query_amount
- init recorder:
- checked (init all elements as False)
- for each query:
- if queried_ID in player_info and checked[queried_ID] == False:
- print(rewards[queried_ID])
- else if queried_ID in player_info:
- else:
- print(ID + “: Are you kidding?”)
完整提交代码
# include<stdio.h>
# include<math.h>
char rewards[3][20] = {"Mystery Award", "Minion", "Chocolate"};
int is_prime(int number){
int sqrt_root = (int)sqrt(number) + 1;
for (int i = 2; i < sqrt_root; i++){
if (number % i == 0) return 0;
}
return 1;
}
void print_reward(int ID, int rank){
if (rank == 1){
printf("%04d: %s\n", ID, rewards[0]);
return;
}
if (is_prime(rank) == 1){
printf("%04d: %s\n", ID, rewards[1]);
return;
}
printf("%04d: %s\n", ID, rewards[2]);
}
int main(){
int player_amount;
int ranking[10000] = {0};
scanf("%d", &player_amount);
int ID;
for (int i = 0; i < player_amount; i++){
scanf("%d", &ID);
ranking[ID] = i + 1;
}
int checked[10000] = {0};
int query_amount;
scanf("%d", &query_amount);
for (int i = 0; i < query_amount; i++){
scanf("%d", &ID);
if (ranking[ID] == 0){
printf("%04d: Are you kidding?\n", ID);
} else if (checked[ID] == 1){
printf("%04d: Checked\n", ID);
} else{
checked[ID] = 1;
print_reward(ID, ranking[ID]);
}
}
return 0;
}
|