公式

A - 山脈の最高峰 / The Highest Peak of the Mountain Range 解説 by kyopro_friends


初心者の方へ


問題文は次のように読み替えることができます。

\(M=\max(A_1. \ldots,A_N)\) とする。 \((A_1,\ldots,A_N)\)\(M\)\(1\) つだけ含まれるなら \(M\) を、 \(2\) つ以上含まれるなら \(0\) を出力せよ」

読み替え後の問題はそのまま実装することができます。

実装例 (C++)

#include<bits/stdc++.h>
using namespace std;

int main(){
  int n;
  cin >> n;
  vector<int>a(n);
  for(int i=0; i<n; i++) cin >> a[i];

  int M=*max_element(a.begin(), a.end());
  int c = count(a.begin(), a.end(), M);
  if(c == 1){
    cout << M << endl;
  }else{
    cout << 0 << endl;
  }
}

実装例 (Python)

N = int(input())
A = list(map(int, input().split()))

M = max(A)
c = A.count(M)
if c == 1:
  print(max(A))
else:
  print(0)

投稿日時:
最終更新: