PAT Basic Level 1059 C语言竞赛 解题思路及AC代码

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值