http://tyvj.cn/p/1001
排序后找到两个数相减,再判断一下质数即可
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[10005];
int main ()
{
int n,k;
scanf("%d%d", &n, &k);
for (int i=0;i<n;i++) scanf("%d", &a[i]);
sort(a,a+n);
int ans = a[n-k] - a[k-1];
if (ans<2)
{
printf("NO\n%d\n", ans);
return 0;
}
else
{
for (int i=2;i<=ans/2;i++)
{
if (ans % i==0)
{
printf("NO\n%d\n", ans);
return 0;
}
}
printf("YES\n%d\n", ans);
}
return 0;
}