PAT-乙-1059 1059 C語言競賽 (20 分)
阿新 • • 發佈:2018-11-03
程式碼
#include <iostream> #include <map> #include <math.h> using namespace std; bool isPrime(int n){ for(int i=2; i<=sqrt(n); i++){ if(n%i==0){ return false; } } return true; } int main() { int n; scanf("%d", &n); map<int, int> m; for(int i=1; i<=n; i++){ int t; cin>>t; m[t] = i; } int q; scanf("%d", &q); for(int i=1; i<=q; i++){ int t; scanf("%d", &t); if(m[t]>0){ if(m[t]==1){ printf("%04d: Mystery Award\n", t); } else if(isPrime(m[t])){ printf("%04d: Minion\n", t); } else{ printf("%04d: Chocolate\n", t); } m[t] = m[t] * -1; } else if(!m[t]){ printf("%04d: Are you kidding?\n", t); } else{ printf("%04d: Checked\n", t); } } return 0; }
註解
1、素數的判定。從1-sqrt(n),看有沒有能整除的,如有,則不是素數。
2、輸入資料用map儲存,key是編號,value是排名。value預設為0,為0即為未出現。
3、check後value值變為負數,因此value為負數輸出checked。