์ƒ์„ธ ์ปจํ…์ธ 

๋ณธ๋ฌธ ์ œ๋ชฉ

[๋ฐฑ์ค€(BOJ)] 11652๋ฒˆ: ์นด๋“œ (์ •๋ ฌ, C++)

PROGRAMMING/Algorithm

by koharin 2021. 8. 6. 20:20

๋ณธ๋ฌธ

728x90
๋ฐ˜์‘ํ˜•

๐Ÿ“„ ๋ฌธ์ œํ’€์ด ๊ณผ์ •


1. ์ž…๋ ฅ ๊ฐ’์˜ ๋ฒ”์œ„

"์ ํ˜€์žˆ๋Š” ์ˆ˜๋Š” -262๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 262๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค."

์ˆ˜์˜ ๋ฒ”์œ„๊ฐ€ integer ๋ฒ”์œ„๋ณด๋‹ค ํฌ๊ณ , ์Œ์ˆ˜๋ฅผ ํฌํ•จํ•˜๊ธฐ ๋•Œ๋ฌธ์— <stdint.h> ํ—ค๋”ํŒŒ์ผ์„ ํฌํ•จํ•˜์—ฌ int64_t ์ž๋ฃŒํ˜•์„ ์‚ฌ์šฉํ–ˆ๋‹ค. ์ž…๋ ฅ ๋ฐ ์ถœ๋ ฅ ์‹œ์—๋„ "%d" ๊ฐ€ ์•„๋‹Œ "%ld"๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. 

 

2. ์ˆ˜์˜ ๋นˆ๋„

์Œ์ˆ˜๋ฅผ ํฌํ•จํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์ธ๋ฑ์Šค๋ฅผ ์ˆ˜๋กœ ์ƒ๊ฐํ•˜๊ณ  ๊ฐ’์„ ์นด์šดํŒ…ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.

๋”ฐ๋ผ์„œ map์„ ์‚ฌ์šฉํ•ด์„œ key์—๋Š” ์ˆ˜๋ฅผ, value๋Š” ์ˆ˜์— ๋Œ€ํ•œ ๋นˆ๋„๋ฅผ ์ €์žฅํ•ด์„œ ์ž…๋ ฅ์„ ๋ฐ›์„ ๋•Œ๋งˆ๋‹ค key์— ๋Œ€ํ•œ value๋ฅผ ์นด์šดํŒ…ํ–ˆ๋‹ค.

์ €์žฅ ํ›„ value ๊ฐ’์ด ๊ฐ€์žฅ ํฐ ๊ฒƒ์„ ์ฐพ์•„์„œ max์— ์ €์žฅํ•˜๊ณ , value์™€ ์ผ์น˜ํ•˜๋Š” key ๊ฐ’์„ ๊ตฌํ•ด์„œ ์ถœ๋ ฅํ•˜๋„๋ก ํ–ˆ๋‹ค.

 

 

๐Ÿ’ป ์ฝ”๋“œ


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

int main(){
    int N; 
    scanf("%d", &N);
    map<int64_t, int> m;

    for(int i=0; i<N; i++){
        int64_t num; scanf("%ld", &num);
        if(m.find(num) == m.end()) m.insert({num, 1});
        else m[num]++;
    }
    int max = 0;
    int64_t result;
    for(auto iter=m.begin(); iter != m.end(); iter++){
        if(iter->second > max) { max = iter->second; result = iter->first; }
    }
    printf("%ld", result);
}

 

 

๐Ÿ–จ ์ œ์ถœ๊ฒฐ๊ณผ


728x90
๋ฐ˜์‘ํ˜•

๊ด€๋ จ๊ธ€ ๋”๋ณด๊ธฐ