※1059 C language contest (20 points)

The C language contest is a joyous contest hosted by the School of Computer Science, Zhejiang University. Since the theme of the competition is for fun, the awarding rules are also very funny:

  • 0. The champion will win a "mystery prize" (such as a huge collection of student research papers...).
  • 1. The ranked 素数student will win the best prize-the little yellow doll!
  • 2. Others will get chocolate.

Given the final ranking of the competition and a series of participant IDs, you have to give the prizes that these participants should receive.

Input format:

Enter the first line to give a positive integer N (≤10^​4), which is the number of participants. The next N rows give the final ranking, and each row gives the ID ( 4 位数字composition) of a contestant in the order of ranking . Next, a positive integer K and K IDs to be queried are given.

Output format:

For each ID to be queried, output in one line ID: 奖品, where the prize is either Mystery Award(Mystery Prize), or Minion(Minion), or Chocolate(Chocolate). If the ID you are looking for is not in the ranking at all , print it Are you kidding?(what's the matter with me?). If the ID has been checked (that is, the prize has already been received), print it ID: Checked(you can't eat more and take up more).

Input sample:

6
1111
6666
8888
1234
5555
0001
6
8888
0001
1111
2222
8888
2222

Sample output:

8888: Minion
0001: Chocolate
1111: Mystery Award
2222: Are you kidding?
8888: Checked
2222: Are you kidding?

Idea:
Due to the time limit of 200ms, string + violence must time out! ! !
Therefore 数组下标, the method used:
set the num array (the array capacity should be large enough, and the array subscript is equivalent to id), and then the array content is the ranking of the id.
In addition, set a flag array to determine whether the id has repeated prizes!

answer:

#include<iostream>
#include<cmath>

using namespace std;

const int N = 1e4;
int num[N];
bool flag[N] = {
    
     false };//判断该id有无重复领取奖品
//判断素数
bool fun1(int x) {
    
    
	if (x == 1) return false;
	for (int i = 2; i < sqrt(x) + 1; i++) {
    
    
		if (x % i == 0) return false;
	}
	return true;
}

int main() {
    
    
	ios::sync_with_stdio(false);
	int n, k, temp;
	scanf("%d", &n);
	for(int i = 0; i < n; i++) {
    
    
		scanf("%d", &temp);
		num[temp] = i + 1;
	}
	scanf("%d", &k);
	for (int i = 0; i < k; i++) {
    
    
		scanf("%d", &temp);
		if (num[temp] == 0) {
    
    //id不存在
			printf("%.4d: Are you kidding?\n", temp);
			continue;
		}
		if (!flag[temp]) {
    
    
			flag[temp] = true;//置真,表领过奖品了
			if (num[temp] == 1){
    
    
				printf("%.4d: Mystery Award\n", temp);
			}
			else if (fun1(num[temp])) {
    
    
				printf("%.4d: Minion\n", temp);
			}
			else {
    
    
				printf("%.4d: Chocolate\n", temp);
			}
		}
		else {
    
    
			printf("%.4d: Checked\n", temp);
		}
	}

	return 0;
}

Note:

  1. It can be used when there are %.4dless than 4 digits. The native printf function of the c language uses less than 4 digits.
  2. Judging the prime number function, if you use x / 2 or sqrt(x), you can, but if you use sqrt(x), you need to add one ----> sqrt(x) + 1otherwise the answer to test point 2 is wrong! !

sqrt(x) + 1 is faster than x / 2! ! ! !

Guess you like

Origin blog.csdn.net/qq_44715943/article/details/115054069