[백준 실버4] 10825 : 국영수

수민이슈·2023년 11월 1일
0

[C++] 코딩테스트

목록 보기
105/116
post-thumbnail

🖊️ 문제

https://www.acmicpc.net/problem/10825


🖊️ 풀이

클래스를 이용해서 정렬하는 아주 간단한 문제!

클래스 내부에 연산자 오버로딩을 통해서 정렬할 수 있도록 했다.

push_back을 통해 사용할 때 객체를 복사생성하는 현상을 막기 위해서 emplace_back을 사용하여 직접 생성자를 호출해주며 삽입해주었다

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

class Student {
public:
	string name;
	int korean;
	int english;
	int math;

public:
	Student(string _name, int _korean, int _english, int _math) :
		name(_name), korean(_korean), english(_english), math(_math) {}

	bool operator< (const Student& rhs) const {
		if (this->korean == rhs.korean && this->english == rhs.english && this->math == rhs.math) return this->name < rhs.name;
		if (this->korean == rhs.korean && this->english == rhs.english) return this->math > rhs.math;
		if (this->korean == rhs.korean) return this->english < rhs.english;
		return this->korean > rhs.korean;
	}

	void Show()
	{
		cout << name << '\n';
	}
};

int main()
{
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(0);

	int n;
	cin >> n;
	
	string name;
	int k, e, m;
	vector<Student> vec;

	for (int i = 0; i < n; i++) {
		cin >> name >> k >> e >> m;
		vec.emplace_back(name, k, e, m);
	}

	sort(vec.begin(), vec.end());

	for (auto& v : vec)
		v.Show();
}

0개의 댓글