Realization of PAT 1059 C Language Competition in C Language

C language competition is a happy competition hosted by computer college. Since the main idea of the competition is for fun, the rules for awarding awards are funny:

• 0. The champion will win a "mystery prize" (such as a huge collection of student research papers...).
• 1. Students ranked as prime will win the best prize - Xiaohuang Doll!
• 2. Others will get chocolate.

Given the final ranking of the competition and the ID of a series of contestants, you should give the prizes that these contestants deserve.

Input format:

Enter the first line and give a positive integer N (<10.4), which is the number of participants. Then N rows give the final ranking, and each row gives the ID of a contestant (four digits). Next, we give a positive integer K and an ID K that needs to be queried.

Output format:

For each ID to be queried, output ID: prize in one line, in which the prize is either Mystery Award, Minion or Chocolate. If the ID is not in the ranking at all, print Are you kidding? If the ID has been checked (that is, the prize has been received), print ID: Checked (not eat more).

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

Output sample:

8888: Minion
0001: Chocolate
1111: Mystery Award
2222: Are you kidding?
8888: Checked
2222: Are you kidding?
#include<stdio.h>
#include<math.h>
#include<stdbool.h>

bool is_prime(int num)
{
if(2 == num || 3 == num ) return true;
int id = sqrt(num);
for(int i = 2 ; i <= id ; i++)
{
if(0 == num % i)
return false;
}
return true;
}

int main()
{
int N = 0; //Number of participants
scanf("%d",&N);
int rank[N];//ranking
for(int i = 0 ; i < N ; i++)
{
scanf("%d",&rank[i]);
}

int K = 0;//Number of people to query
scanf("%d",&K);
int id[K];//ID to query
int mark[K];//What is the number of tags?
for(int i = 0 ; i < K ;i++)
{
scanf("%d",&id[i]);
}

for(int i = 0 ; i < K ;i++)
{
mark[i] = -1;	//Did not appear
for(int j = 0 ; j < N ;j++)
{
if(rank[j] == id[i])
{
if(0 == j)	//The first
{
mark[i] = 1;
break;
}
else if(is_prime(j+1))//Proxime accessit
{
mark[i] = 2;
break;
}
else	//Third
{
mark[i] = 3;
break;
}
}
}
}
for(int i = 0;i < K ; i++)
{
for(int j = i+1; j < K ; j++)
{
if(mark[i] != -1 && id[i] == id[j])
mark[j] = 0;
}
if(-1 == mark[i])
printf("%04d: Are you kidding?\n",id[i]);
else if(0 == mark[i])
printf("%04d: Checked\n",id[i]);
else if(1 == mark[i])
printf("%04d: Mystery Award\n",id[i]);
else if(2 == mark[i])
printf("%04d: Minion\n",id[i]);
else if(3 == mark[i])
printf("%04d: Chocolate\n",id[i]);
}
return 0;
}

Tags: C

Posted on Wed, 09 Oct 2019 20:24:01 -0700 by Yetalia