CSP201612-1 中间数
1 // 2 // main.cpp 3 // CSP201612-1 中间数 4 // 5 // Created by sylvia on 2021/11/4. 6 // Copyright © 2021 apple. All rights reserved. 7 // 8 9 #include <iostream> 10 #include <algorithm> 11 using namespace std; 12 #define M 1000+2 13 int a[M]; 14 void fun(int i,int j,int mid){ 15 while (a[i]==a[j]){ 16 i--;j++; 17 } 18 if((a[i]==a[mid]&&a[j]!=a[mid])||(a[j]==a[mid]&&a[i]!=a[mid])) cout<<-1<<endl; 19 else cout<<a[mid]<<endl; 20 } 21 int main(){ 22 int n; 23 cin>>n; 24 for (int i=0;i<n;i++){ 25 cin>>a[i]; 26 } 27 sort(a,a+n); 28 int mid=n/2; 29 if (n%2){ //奇数 30 fun(mid-1,mid+1,mid); 31 } 32 else if(!(n%2)){ 33 fun(mid-1,mid,mid); 34 } 35 return 0; 36 }View Code