PAT (Advanced Level) Practice A1116 Come on! Let's C (20 分)(C++)(甲級)(質數)

原題鏈接

#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;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章