티스토리 뷰

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;
}
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
글 보관함