题目:1059 C语言竞赛 (20 分)
来源:PAT (Basic Level) Practice
传送门 1059 C语言竞赛
题面

思路:分类讨论,注意格式化输出与素数筛
Code
点击查看代码
#include <iostream>
#include <iomanip>
using namespace std;
int id[20000];
int isPrime(int x) {
if (x == 1)return false;
for (int i = 2; i * i <= x; i++) {
if (x % i == 0)return false;
}
return true;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n, k, x;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> x;
id[x] = i;
}
cin >> k;
for (int i = 0; i < k; i++) {
cin >> x;
if (id[x] > 0) {
if (id[x] == 1) {
cout << setfill('0') << setw(4) << x << ": Mystery Award" << "n";
id[x] = -1;
}
else if (isPrime(id[x])) {
cout << setfill('0') << setw(4) << x << ": Minion" << "n";
id[x] = -1;
}
else {
cout << setfill('0') << setw(4) << x << ": Chocolate" << "n";
id[x] = -1;
}
}
else if (id[x] == -1) {
cout << setfill('0') << setw(4) << x << ": Checked" << "n";
}
else cout << setfill('0') << setw(4) << x << ": Are you kidding?" << "n";
}
return 0;
}