本文主要是介绍【PAT】1116. Come on! Let's C (20)【素数表-埃拉托斯特尼筛法】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目描述
“Let’s C” is a popular and fun programming contest hosted by the College of Computer Science and Technology, Zhejiang University. Since the idea of the contest is for fun, the award rules are funny as the following:
0、 The Champion will receive a “Mystery Award” (such as a BIG collection of students’ research papers…).
1、 Those who ranked as a prime number will receive the best award – the Minions (小黄人)!
2、 Everyone else will receive chocolates.
Given the final ranklist and a sequence of contestant ID’s, you are supposed to tell the corresponding awards.
翻译:“Let’s C” 是一个由浙江大学计软院组织的有名的和有趣的程序比赛。由于这个比赛的目的是为了有趣,所以颁奖规则按照以下方式办理:
0.冠军将会获得一份"神秘的奖项"(类似于学生论文的大集合。。。)
1.那些排名为素数的人将获得最佳奖——小黄人!
2.其他人将获得巧克力。
给定最终的排名和一个参赛者ID序列,你需要说出对应的奖项。
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤104), the total number of contestants. Then N lines of the ranklist follow, each in order gives a contestant’s ID (a 4-digit number). After the ranklist, there is a positive integer K followed by K query ID’s.
翻译:每个输入文件包含一组测试数据。对于每组输入数据,第一行包括一个正整数N(≤104),表示参赛总人数。接下来有N行排名表,每一行按照排名顺序给出一个参赛者的ID(一个四位数字)。在排名表后,有一回正整数K,跟着K给查询的ID。
Output Specification:
For each query, print in a line ID: award where the award is Mystery Award, or Minion, or Chocolate. If the ID is not in the ranklist, print Are you kidding? instead. If the ID has been checked before, print ID: Checked.
翻译:对于每个查询,输出一行,ID:奖品例如Mystery Award, 或 Minion, 或 Chocolate。如果ID不在排名表上,则输出Are you kidding? 。如果ID之前已经被检索过了,输出print ID: Checked。
Sample Input:
6
1111
6666
8888
1234
5555
0001
6
8888
0001
1111
2222
8888
2222
Sample Output:
8888: Minion
0001: Chocolate
1111: Mystery Award
2222: Are you kidding?
8888: Checked
2222: Are you kidding?
解题思路
通过埃拉托斯特尼筛法筛出素数表,然后进行各种判断。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<algorithm>
#define INF 99999999
#define bug puts("Hello\n")
using namespace std;
bool prime[10010],visited[10010];
int Rank[10010],N;
void fun(){for(int i=1;i<=N;i++)prime[i]=true,visited[i]=false;for(int i=2;i<=N;i++){if(prime[i]){for(int j=i+i;j<=N;j+=i){prime[j]=false;}}}
}
int main(){scanf("%d",&N);fun();int a;for(int i=1;i<=N;i++){scanf("%d",&a);Rank[a]=i; }int K;scanf("%d",&K);for(int i=0;i<K;i++){scanf("%d",&a);printf("%04d: ",a);if(visited[a]==false&&Rank[a]!=0){if(Rank[a]==1)printf("Mystery Award\n");else if(prime[Rank[a]]==true)printf("Minion\n");else printf("Chocolate\n");visited[a]=true;}else if(Rank[a]==0)printf("Are you kidding?\n");else printf("Checked\n");}return 0;
}
这篇关于【PAT】1116. Come on! Let's C (20)【素数表-埃拉托斯特尼筛法】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!