TYVJ 1001 第k极值 by C++
1 #include<iostream> 2 using namespace std; 3 const int maxn=10000; 4 int a[maxn]; 5 int n,k; 6 bool prime(int now) 7 { 8 if (now<2) return false; 9 if (now==2) return true; 10 for (int i=2;i<now;i++){if (now%i==0) return false;} 11 return true; 12 } 13 void get_input() 14 { 15 cin >> n >> k; 16 for (int i=0;i<n;i++) cin >> a[i]; 17 } 18 void sort(int l,int r) 19 { 20 int i,j,mid; 21 i=l;j=r;mid=a[(l+r)/2]; 22 while (i<=j){ 23 while (a[i]<mid) i++; 24 while (a[j]>mid) j--; 25 if (i<=j){ 26 int tmp; 27 tmp=a[i];a[i++]=a[j];a[j--]=tmp; 28 } 29 } 30 if (i<r) sort(i,r); 31 if (l<j) sort(l,j); 32 } 33 int main() 34 { 35 get_input(); 36 sort(0,n-1); 37 int min,max; 38 min=a[k-1]; 39 max=a[n-k]; 40 cout << (prime(max-min)?"YES":"NO") << endl; 41 cout << max-min << endl; 42 }
发现C++里把==错写成=相当杯具