[백준] 1302 베스트셀러 c++

2022. 9. 13. 23:48·🍞 Algorithm/Baekjoon
https://www.acmicpc.net/problem/1302 
 

1302번: 베스트셀러

첫째 줄에 오늘 하루 동안 팔린 책의 개수 N이 주어진다. 이 값은 1,000보다 작거나 같은 자연수이다. 둘째부터 N개의 줄에 책의 제목이 입력으로 들어온다. 책의 제목의 길이는 50보다 작거나 같고

www.acmicpc.net

 

풀이과정

string을 key로 하고, int를 value로 한다.
팔린 책의 개수인 second 개수가 가장 큰 값을 res에 넣는다.

가장 많이 팔린 책이 여러 개일 경우에는 사전 순으로 가장 앞서는 제목을 출력해야 되니  
팔린 책의 개수인 second 개수가 res일 경우에 first인 key를 출력해주고 함수를 끝낸다.  
(map은 삽입이 되면서 오름차순으로 자동 정렬된다.)

 

반복문 데이터 접근

- 인덱스 기반

for(auto iter = m.begin(); iter != m.end(); iter++){
  cout << iter->first << " " << iter->second << "\n";
}

 

- 반복문 기반

for(auto iter:m){
  cout << iter.first << " " << iter.second << "\n";
}

 

방법1 (인덱스 기반)

#include <algorithm>
#include <iostream>
#include <string>
#include <map>
using namespace std;

int N;
string book;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL), cout.tie(NULL);

	cin >> N;
	map <string, int> m;

	while(N--){
		cin >> book;
		m[book]++;
	}
	int res = 0;
	for (auto i = m.begin(); i != m.end(); i++) {
		res = max(res, i->second);
	}
	for (auto i = m.begin(); i != m.end(); i++) {
		if (res == i->second) {
			cout << i->first;
			return 0;
		}
	}
}

 

방법2 (반복문 기반)

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <string>
#include <map>
using namespace std;

int N;
string book;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL), cout.tie(NULL);

	cin >> N;
	map <string, int> m;

	while(N--){
		cin >> book;
		m[book]++;
	}
	int res = 0;
	for (auto i:m) {
		res = max(res, i.second);
	}
	for (auto i:m) {
		if (res == i.second) {
			cout << i.first;
			return 0;
		}
	}
}
저작자표시 (새창열림)

'🍞 Algorithm > Baekjoon' 카테고리의 다른 글

[백준] 14425 문자열 집합 c++  (0) 2022.09.13
[백준] 1620 나는야 포켓몬 마스터 이다솜 c++  (0) 2022.09.13
[백준] 4803 트리 c++  (0) 2022.09.05
[백준] 15809 전국시대 c++  (0) 2022.09.05
[백준] 1939 중량제한 c++  (0) 2022.09.05
'🍞 Algorithm/Baekjoon' 카테고리의 다른 글
  • [백준] 14425 문자열 집합 c++
  • [백준] 1620 나는야 포켓몬 마스터 이다솜 c++
  • [백준] 4803 트리 c++
  • [백준] 15809 전국시대 c++
박빵이
박빵이
2025년에도 갓생살기
  • 박빵이
    기억보다 기록
    박빵이
  • 전체
    오늘
    어제
    • 분류 전체보기 (337)
      • 🍞 FrontEnd (97)
        • HTML+CSS (4)
        • JavaScript (17)
        • TypeScript (4)
        • React (52)
        • Next.js (2)
        • Android (15)
      • 🍞 BackEnd (24)
        • Java (15)
        • Node.js (6)
        • Spring (1)
      • 🍞 Cloud & Infra (0)
        • AWS SAA (0)
        • Microsoft Azure (0)
      • 🍞 Algorithm (147)
        • C++ (4)
        • Baekjoon (41)
        • Programmers (97)
      • 🍞 Computer Science (18)
        • 운영체제 (1)
        • 데이터 통신 (6)
        • 네트워크 (6)
        • 데이터베이스 (1)
      • 🍞 대외활동 & 부트캠프 (42)
        • 삼성 청년 SW 아카데미 (1)
        • LG유플러스 유레카 (0)
        • 한국대학생IT경영학회 (1)
        • IT연합동아리 UMC (17)
        • 길벗 블로깅 멘토 (18)
        • IT연합동아리 피로그래밍 (3)
        • 개발 컨퍼런스 (2)
  • 블로그 메뉴

    • Admin
  • 링크

    • GitHub
  • 인기 글

  • 태그

    C++
    길벗 블로깅 멘토링
    Android
    코틀린
    Java
    안드로이드
    map
    유니온파인드
    Front
    알고리즘
    코딩자율학습
    umc
    위상정렬
    백준
    JavaScript
    level1
    프로그래머스
    길벗 블로깅 멘토
    react
    level2
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
박빵이
[백준] 1302 베스트셀러 c++
상단으로

티스토리툴바