第一遍超时了之后再看一眼题很快就有思路了,用数组下标标记就行
但是出现了小错误
1.注意是素数,不是奇数
2.改过来后依旧有一个点不得分,几经排查
我判断这个数是否在数组中的时候,判断不等于-1就过不去,判断是否为1就通过了,不知道这个样例测试到底是个啥数字?正常来说,这个数字不是1那肯定就是-1啊,,迷茫
更新
解决了,是memset这里出了问题,赋值的区间不能直接用10000,要用sizeof(ans)
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <string>
#include <cctype>
#include <vector>
#include <cstring>
#include <cmath>
using namespace std;
struct student{
string id;
string award;
int rank;
int check;
};
int n,m;
int isprime(int a){
int b = sqrt((double)a);
for(int i = 2;i <= b;i++){
if(a%i==0){
return 0;
}
}
return 1;
}
int main()
{
cin >> n;
vector<student> stu(10000);
int ans[10000];
memset(ans,-1,10000);
for(int i=1; i<=n; i++){
string a;
cin >> a;
int num = stoi(a);
ans[num] = 1;
stu[num].id = a;
stu[num].rank = i;
if(i==1) stu[num].award = "Mystery Award";
else if(isprime(i)) stu[num].award = "Minion";
else stu[num].award = "Chocolate";
}
cin >> m;
for(int i=0; i<m; i++){
string a;
cin >> a;
int num = stoi(a);
if(ans[num]==1){
if(stu[num].check==0){
cout << a <<": "<< stu[num].award<< endl;
stu[num].check = 1;
}
else cout << a <<": Checked"<<endl;
}
else cout << a << ": Are you kidding?"<< endl;
}
return 0;
}
超时代码
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <string>
#include <cctype>
#include <vector>
#include <cstring>
#include <map>
using namespace std;
struct student{
string id;
string award;
int rank;
int check;
};
int n,m;
int main()
{
cin >> n;
vector<student> stu(n+1);
for(int i=1; i<=n; i++){
string a;
cin >> a;
stu[i].id = a;
stu[i].rank = i;
if(i==1) stu[i].award = "Mystery Award";
else if(i%2!=0) stu[i].award = "Minion";
else stu[i].award = "Chocolate";
}
cin >> m;
for(int i=0; i<m; i++){
string a;
cin >> a;
int flag = 0;
for(int j=1; j<=n; j++){
//cout << stu[j].id << " a " << stu[j].check << endl;
if(a==stu[j].id&&stu[j].check==0){
cout << a <<": "<< stu[j].award<< endl;
stu[j].check = 1;
flag = 1;
break;
}
if(a==stu[j].id&&stu[j].check==1){
cout << a <<": Checked"<<endl;
flag = 1;
break;
}
}
if(flag==0) cout << a << ": Are you kidding?"<< endl;
}
return 0;
}