PAT (Advanced Level) Practice A1116 Come on! Let's C (20 分)(C++)(甲级)(质数)

版权声明:假装有个原创声明……虽然少许博文不属于完全原创,但也是自己辛辛苦苦总结的,转载请注明出处,感谢! https://blog.csdn.net/m0_37454852/article/details/88425872

原题链接

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
using namespace std;

const int MAX = 10010, INF = 1<<30;

typedef struct Student
{
    bool flag;
    string award;
    Student(){}
    Student(int _flag, string _award)
    {
        flag = _flag;
        award = _award;
    }
}Student;

int N, K, ID;
Student S[MAX] = Student(false, "Are you kidding?");//全部初始化为没有输入,r u kidding
int Prime[MAX] = {0};
bool p[MAX] = {0};//是否是质数的标志

void initPrime()//埃式筛法求质数
{
    fill(p, p+MAX, 1);
    for(int i=2; i<MAX; i++)
    {
        if(p[i])
        {
            for(int j=i+i; j<MAX; j+=i)
            {
                p[j] = 0;
            }
        }
    }
}

int main()
{
    initPrime();
    scanf("%d", &N);
    scanf("%d", &ID);
    S[ID] = Student(true, "Mystery Award");//冠军
    for(int i=2; i<=N; i++)
    {
        scanf("%d", &ID);
        if(p[i]) S[ID] = Student(true, "Minion");//是质数
        else S[ID] = Student(true, "Chocolate");//不是质数
    }
    scanf("%d", &K);
    for(int i=0; i<K; i++)
    {
        scanf("%d", &ID);
        printf("%04d: %s\n", ID, S[ID].award.c_str());
        if(S[ID].flag) S[ID].award = "Checked";//访问过之后修改
    }
	return 0;
}



猜你喜欢

转载自blog.csdn.net/m0_37454852/article/details/88425872
今日推荐