PAT 乙级 1059 C语言竞赛 v1.0
1. 题目简述及在线测试位置
1.1 给定学生考号和名次信息,按要求输出获得的奖品
1.2 在线测试位置: 1059 C语言竞赛
2. 基本思路
2.1 本题考查了数据结构的概念:数据在计算机中如何存储?良好的存储方式 决定了 良好的数据处理效率。本文使用了结构体数组:数组下标代表学生ID、结构体中index字段代表名次、结构体中Received字段代表是否可以获得奖品
struct Info
{
int index=0; //名次,默认0表示无效
bool Received=true; //是否有资格获得奖品,默认为是
}a[MAX]; //数组a的下标代表学生ID
2.2 据此存储数据,然后按输入条件进行判定即可
3. 完整AC代码
#include <string> //stoi()
#include <iostream>
using namespace std;
#define MAX 10001
struct Info
{
int index=0; //名次,默认0表示无效
bool Received=true; //是否有资格获得奖品,默认为是
};
bool isPrime(int Number);
int main()
{
struct Info a[MAX]; //数组a的下标代表学生ID
int N, ID;
cin >> N;
for (int i = 1; i <= N; i++)
{
cin >> ID;
a[ID].index = i;
}
int Query; //查询次数
string Identi; //查询ID
cin >> Query;
while (Query--)
{
cin >> Identi;
if (!a[stoi(Identi)].index) //无效名次
cout << Identi << ": Are you kidding?" << endl;
else if (!a[stoi(Identi)].Received) //已收到礼物
cout << Identi << ": Checked" << endl;
else if (a[stoi(Identi)].index == 1 )
{
cout << Identi << ": Mystery Award" << endl;
a[stoi(Identi)].Received = false;
}
else if ( isPrime(a[stoi(Identi)].index) )
{
cout << Identi << ": Minion" << endl;
a[stoi(Identi)].Received = false;
}
else if ( !isPrime(a[stoi(Identi)].index) )
{
cout << Identi << ": Chocolate" << endl;
a[stoi(Identi)].Received = false;
}
}
return 0;
}
bool isPrime(int Number)
{
int N = Number;
if (N > 1)
{
for (int i = 2; i * i <= N; i++)
if (N % i==0)
return false;
}
else if(N == 1)
return false;
return true;
}