0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

C++入門 AtCoder Programming Guide for beginners (APG4b)

Posted at

EX23 - 最頻値
https://atcoder.jp/contests/apg4b/tasks/APG4b_bz

image.png

連想配列の練習問題。
map関数を使う。

map関数の使い方

map<"型(int)", "型(int)"> "配列名(A)" ; //関数宣言
A["番号(4)"]="入力値(57)"; //入力
A["番号(2)"]="入力値(31)"; //入力
int a=A[2]; //呼び出し aに31が代入される
int a=A[10]; //宣言してない配列呼び出し A[10]=0で初期化され、a=0が代入されるはず

バケット法を関数でやれる。
今回の問題ではA[i]の数値を番号としてラベルし、その時の値をインクリメントするようにする。

# include <bits/stdc++.h>
# include <math.h>
using namespace std;
 
int main() {
  long long N;
  long long A[110000];
  int max=0;
  int ans;
  map<long, long> B ;
  cin>>N;
  for(int i=0;i<N;i++){
      cin>>A[i];
      B[A[i]] = B[A[i]]+1;  //A[i]の値をB配列に入力し、カウントアップする
      if(max<B[A[i]]){
          max=B[A[i]];  //最大値の個数を記録する。
          ans=A[i];  //最大値を記録する。
      }
  }
cout<<ans<<" "<<B[ans]<<endl;
}

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?