After the first timeout, it's a quick idea to look at the question again. Just mark it with array subscripts.
But there was a slight mistake.
1. Note that it's a prime, not an odd number.
2. After the change, there is still a point that can not be divided, after several investigations.
When I judge whether this number is in the array, judgment is not equal to - 1, judgment is not passable, judgment is 1 passed, do not know what the exact number of this sample test is? Normally, this number is not 1. That must be - 1. Ah, confused.
#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; }
Timeout code
#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; }