背景
成成第一次模拟赛 第一道
描述
给定一个长度为N(0<n<=10000)的序列,保证每一个序列中的数字a[i]是小于maxlongint的非负整数 ,编程要求求出整个序列中第k大的数字减去第k小的数字的值m,并判断m是否为质数。(0<k<=n)
输入格式
输入格式:
第一行为2个数n,k(含义如上题)
第二行为n个数,表示这个序列
第一行为2个数n,k(含义如上题)
第二行为n个数,表示这个序列
输出格式
输出格式:
如果m为质数则
第一行为'YES'(没有引号)
第二行为这个数m
否则
第一行为'NO'
第二行为这个数m
如果m为质数则
第一行为'YES'(没有引号)
第二行为这个数m
否则
第一行为'NO'
第二行为这个数m
测试样例1
输入
5 2
1 2 3 4 5
输出
YES
2
备注
对于第K大的详细解释:
如果一个序列为1 2 2 2 2 3
第1大 为3
第2大 为2
第3大 为2
第4大 为2
第5大 为1
第K小与上例相反
如果一个序列为1 2 2 2 2 3
第1大 为3
第2大 为2
第3大 为2
第4大 为2
第5大 为1
第K小与上例相反
另外需要注意的是
最小的质数是2,如果小于2的话,请直接输出NO
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int a[],m,N,K;
int check(int x){
if(x<=) return ;
for(int i=;i<=sqrt(x);i++)//当x=2或3时,sqrt都为1
if(x%i==) return ;
return ;
}
int main(){
// freopen("01.txt","r",stdin);
scanf("%d%d",&N,&K);
for(int i=;i<=N;i++)
scanf("%d",&a[i]); sort(a+,a+N+);
m=a[N-K+]-a[K]; if(check())
cout<<"YES"<<endl<<m<<endl;
else
cout<<"NO"<<endl<<m<<endl;
return ;
}