CCF 2016-12-1 中间数 PYTHON

n=input()
n=eval(n)
a=input().split()
a=[eval(item) for item in a]
a.sort()
count_small=0
count_big=0
if n%2==1:
    mid=int(n//2)
    for item in a[:mid]:
        if item!=a[mid]:
            count_small+=1
    for item in a[mid+1:]:
        if item != a[mid]:
            count_big += 1
    if count_small==count_big:
        print(a[mid])
    else:
        print(-1)

else:#偶数数组
    mid1=int(n/2-1)
    mid2=int(n/2)
    if a[mid1]!=a[mid2]:
        print(-1)
    else:
        for item in a[:mid1]:
            if item!=a[mid1]:
                count_small+=1
        for item in a[mid2:]:
            if item!=a[mid2]:
                count_big+=1
        if count_small==count_big:
            print(a[mid1])
        else:
            print(-1)

相关文章:

  • 2021-04-14
  • 2021-04-27
  • 2021-11-28
  • 2021-11-18
  • 2021-12-26
  • 2021-11-28
  • 2022-12-23
  • 2021-05-08
猜你喜欢
  • 2021-09-02
  • 2022-12-23
  • 2022-01-07
  • 2021-12-04
  • 2021-10-02
  • 2022-02-10
  • 2021-04-28
相关资源
相似解决方案