公式
A - 山脈の最高峰 / The Highest Peak of the Mountain Range 解説
by
A - 山脈の最高峰 / The Highest Peak of the Mountain Range 解説
by
kyopro_friends
初心者の方へ
- AtCoder をはじめたばかりで何をしたらよいか分からない方は、まずは practice contest の問題A「Welcome to AtCoder」を解いてみてください。基本的な入出力の方法が載っています。
- また、プログラミングコンテストの問題に慣れていない方は、AtCoder Beginners Selection の問題をいくつか解いてみることをおすすめします。
- C++入門 AtCoder Programming Guide for beginners (APG4b) は、競技プログラミングのための C++ 入門用コンテンツです。
- Python入門 AtCoder Programming Guide for beginners (APG4bPython) は、競技プログラミングのための Python 入門用コンテンツです。
問題文は次のように読み替えることができます。
「 \(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)
投稿日時:
最終更新:
