题目链接:http://www.tyvj.cn/p/1001
没什么好讲的,,不过因为一个小问题一直WA,,,,素数的时候一定记得是sqrt(fabs(x))
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 #include<vector> 7 #include<string> 8 #include<cctype> 9 using namespace std; 10 const int maxn=10000+10; 11 int a[maxn]; 12 bool cmp1(const int a,const int b) 13 { 14 return a>b; 15 } 16 bool cmp2(const int a,const int b) 17 { 18 return a<b; 19 } 20 bool judge(int x) 21 { 22 bool flag=false; 23 for(int i=2;i<=sqrt(fabs(x));i++) 24 { 25 if(x%i==0) 26 { 27 flag=true; 28 } 29 } 30 return flag; 31 } 32 int main() 33 { 34 int n,k; 35 while(cin>>n>>k) 36 { 37 for(int i=0;i<n;i++) 38 cin>>a[i]; 39 sort(a,a+n,cmp1); 40 int k1=a[k-1]; 41 sort(a,a+n,cmp2); 42 int k2=a[k-1]; 43 int m=k1-k2; 44 if(judge(m)) 45 cout<<"NO"<<endl; 46 else 47 cout<<"YES"<<endl; 48 cout<<m<<endl; 49 } 50 return 0; 51 }