当前位置:   article > 正文

PAT-乙-1059 1059 C语言竞赛 (20 分)_1059 c语言竞赛 (20 分)java

1059 c语言竞赛 (20 分)java

在这里插入图片描述
在这里插入图片描述

代码

#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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

注解

1、素数的判定。从1-sqrt(n),看有没有能整除的,如有,则不是素数。
2、输入数据用map存储,key是编号,value是排名。value默认为0,为0即为未出现。
3、check后value值变为负数,因此value为负数输出checked。

结果

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/713114
推荐阅读
相关标签
  

闽ICP备14008679号