公式
B - クラス委員長の選出 / Election of the Class President 解説
by
B - クラス委員長の選出 / Election of the Class President 解説
by
kyopro_friends
問題文の指示通りの手続きで答えを求めればよいです。つまり、出席番号 \(1\) の生徒を除いて点数が最大の生徒を求め、出席番号 \(1\) の生徒と比較すればよいです。
実装例 (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 score = -1;
int index = -1;
for(int i=1; i<n; i++){
if(a[i] > score){
score = a[i];
index = i;
}
}
if(score > a[0]){
cout << index + 1 << endl;
}else{
cout << -1 << endl;
}
}
なお、問題の条件を整理すると「\(N\) 人の生徒のうち、点数が最大の生徒(複数いる場合はそのうち出席番号が最も小さな生徒)を求め、その出席番号が \(1\) なら -1 を、そうでなければ出席番号を出力せよ」となります。この方針に従うと以下のように実装することもできます。
実装例 (Python)
N = int(input())
A = list(map(int, input().split()))
M = max(A)
pos = A.index(M)
if pos == 0:
print(-1)
else:
print(pos + 1)
投稿日時:
最終更新:
