Algorithm/유형별 : Search
[C++] [탐색] 백준 1302번 : 베스트셀러
쟌쥰
2020. 2. 2. 14:08
https://www.acmicpc.net/problem/1302
1302번: 베스트셀러
첫째 줄에 오늘 하루 동안 팔린 책의 개수 N이 주어진다. 이 값은 1,000보다 작거나 같은 자연수이다. 둘째부터 N개의 줄에 책의 제목이 입력으로 들어온다. 책의 제목의 길이는 50보다 작거나 같고, 알파벳 소문자로만 이루어져 있다.
www.acmicpc.net
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
struct BOOK {
string name;
int sell = 1;
};
bool cmp(BOOK f, BOOK s) {
if (f.sell == s.sell)
return f.name < s.name;
else
return f.sell > s.sell;
}
int main(void) {
int N;
//입력
cin >> N;
BOOK *book = new BOOK[N];
for (int i = 0; i < N; i++)
cin >> book[i].name;
//책의 판매량을 배열에서 가장 앞에있는 책에 ++
for (int i = 1; i < N; i++) {
for (int j = 0; j < i; j++) {
if (book[j].name == book[i].name) {
book[j].sell++;
break;
}
}
}
//판매순, 사전순 정렬
sort(book, book + N, cmp);
cout << book[0].name << endl;
}