抽奖,顺带考察了下怎么求素数。
分清情况讨论就好。
#include <iostream>
#include <map>
#include <string>
#define MAX 10010
using namespace std;
map <int, int> ID2RANK;
int Rank[MAX];
int n, m;
string Champion = "Mystery Award";
string Prime = "Minion";
string Everyone = "Chocolate";
string NoEntry = "Are you kidding?";
string Checked = "Checked";
void CheckReward(int ID) {
switch (ID2RANK[ID])
{
case 0:printf("%04d: %s\n", ID, &NoEntry[0]); break;
case 1:printf("%04d: %s\n", ID, &Champion[0]); ID2RANK[ID] = 4; break;
case 2:printf("%04d: %s\n", ID, &Everyone[0]); ID2RANK[ID] = 4; break;
case 3:printf("%04d: %s\n", ID, &Prime[0]); ID2RANK[ID] = 4; break;
case 4:printf("%04d: %s\n", ID, &Checked[0]); break;
default:
break;
}
}
bool isPrime(int num) {
if (num <= 1) return false;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0)
return false;
}
return true;
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> Rank[i];
if(i==1)
ID2RANK[Rank[i]] = 1;
else if (isPrime(i))
ID2RANK[Rank[i]] = 3;
else
ID2RANK[Rank[i]] = 2;
}
cin >> m;
int temp;
for (int i = 0; i < m; i++) {
cin >> temp;
CheckReward(temp);
}
return 0;
}