PAT B1059 C language competition (20 points)

Insert picture description here
It can be easily solved with map

#include <cstdio>
#include <iostream>
#include <string>
#include <map>
#include <cmath>
using namespace std;

map<string,int> r;
map<string,int> t;

bool isprime(int n){
    
    
	int sqr = (int)sqrt(1.0*n);
	for(int i=2; i<=sqr; i++){
    
    
		if(n % i == 0) return false;
	}
	return true;
}

void prize(int r){
    
    
	if(r == 1){
    
    
		printf("Mystery Award\n");
		return;
	}else if(isprime(r)){
    
    
		printf("Minion\n");
		return;
	}else{
    
    
		printf("Chocolate\n");
		return;
	}
}

int main(){
    
    
	int n;
	scanf("%d", &n);
	
	for(int i=1; i<=n; i++){
    
    
		string str;
		cin >> str;
		r[str] = i;
	}
	
	int k;
	scanf("%d", &k);
	
	for(int i=1; i<=k; i++){
    
    
		string str;
		cin >> str;
		t[str]++;
		if(r[str] == 0){
    
    
			printf("%s: Are you kidding?\n", str.c_str());
			continue;
		}
		if(t[str] == 1){
    
    
			printf("%s: ", str.c_str());
			prize(r[str]);
		}else{
    
    
			printf("%s: Checked\n", str.c_str());
		}
	}
	
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45964844/article/details/113745632