!!!判断素数,i <= sqrt(n); 是小于等于,如果只是小于是错的!
这是map 方法。
#include <iostream>
#include <cmath>
#include <string>
#include <map>
using namespace std;
bool isprime(int n){
int flag = 1;
if(n == 1 || n == 0) flag = 0;
for(int i = 2; i <= sqrt(n); i++)
if(n % i == 0) flag = 0;
return flag;
}
int main(){
string id;
int N, M;
cin >> N;
map<string, int> ma;
for(int i = 1; i <= N; i++){
cin >> id;
ma[id] = i;
}
cin >> M;
for(int i = 0; i < M; i++){
cin >> id;
if(ma[id] == -1)
cout << id << ": Checked" << endl;
else if(ma[id] == 1){
cout << id << ": Mystery Award" << endl;
ma[id] = -1;
}else if(isprime( ma[id] )){
cout << id << ": Minion" << endl;
ma[id] = -1;
}else if(ma[id] == 0)
cout << id << ": Are you kidding?" << endl;
else{
cout << id << ": Chocolate" << endl;
ma[id] = -1;
}
}
return 0;
}
这是数组方法,都一样。
#include <iostream>
#include <cmath>
using namespace std;
int isprime(int n){
int flag = 1;
if(n <= 1) flag = 0;
for(int i = 2; i <= sqrt(n); i++){
if(n % i == 0) flag = 0;
}
return flag;
}
int main(){
int N, M, id, ma[10005]={0};
cin >> N;
for(int i = 1; i <= N; i++){
cin >> id;
ma[id] = i;
}
cin >> M;
for(int i = 0; i < M; i++){
cin >> id;
if(ma[id] == 0)
printf("%04d: Are you kidding?\n", id);
else if(ma[id] == -1)
printf("%04d: Checked\n",id);
else if(ma[id] == 1){
printf("%04d: Mystery Award\n", id);
ma[id] = -1;
}else if(isprime( ma[id] )){
printf("%04d: Minion\n", id);
ma[id] = -1;
}else{
printf("%04d: Chocolate\n", id);
ma[id] = -1;
}
}
return 0;
}