백준 - 10825번 국영수_정렬

phoenixKim·2021년 7월 28일
0

백준 알고리즘

목록 보기
7/174

맞왜틀

: endl; 은 '\n' 보다 느림.

언제 품

: 220902

생각할 점.

: pair 타입으로 넣을까? 생각을 하게 되었는데, 복잡함..
여러가지 자료구조를 하나의 유저 타입으로 만들어야 함을 생각해내야 함!

알게 된점

1. compare(a,b) 함수

compare 함수내의 파라미터 값을 2개로 해서 비교를 하는데,
bool compare(a, b) 일 때 증가하는 방식이라면
어떻게 생각하냐면 첫번째 매개변수인 a가 두번째 매개변수인 b보다 작다라는
생각을 가지고 접근하면 된다. -> 오름차순
반대로 감소하는 방식이라면 먼저 온 a가 다음에 온 b보다 값이 크다고 한다면
다음에 오는 값들이 감소되는 것이다. -> 내림 차순

값이 증가하는 방식

bool compare(a, b)
{
return a < b;
}

값이 감소하는 방식

bool compare(a, b)
{
return a > b;
}

풀이

  • 2가지가 잇다. 일단 정렬해야해
    1) 구조체를 이용한 정렬
    2) 클래스를 이용한 정렬

소스코드

두번째 문제 풀이

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

struct stu
{
	string a;
	int b;
	int c;
	int d;

};

bool check(stu& _a, stu& _b)
{
	if (_a.b > _b.b)
		return true;
	else if (_a.b == _b.b)
	{
		if (_a.c < _b.c)
			return true;
		else if (_a.c == _b.c)
		{
			if (_a.d > _b.d)
				return true;
			else if (_a.d == _b.d)
			{
				if (_a.a < _b.a)
					return true;
			}
		}
	}
	return false;
}

int main()
{
	// 국어 내림차순
	// 국어 동일하면, 영어 오름차순으로 
	// 국어와 영어 동일하면 수학 내림차순으로 
	// 모든 점수가 동일하면, 이름이 사전순 오름차순.

	int n;
	cin >> n;

	vector<stu>v(n);
	for (int i = 0; i < n; ++i)
	{
		cin >> v[i].a >> v[i].b >> v[i].c >> v[i].d;
	}
	/*
	cout << "out" << endl;
	for (int i = 0; i < n; ++i)
	{
		cout << v[i].a <<  " "<<  v[i].b 
			<< " "<< v[i].c << " "<< v[i].d << endl;
	}
	*/

	sort(v.begin(), v.end(), check);

	//cout << "out" << endl;
	for (int i = 0; i < n; ++i)
	{
		cout << v[i].a << '\n';			
	}

}

1.소스 코드_구조체를 이용한

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

struct student
{
	string mName;
	int mKor;
	int mEng;
	int mMath;
};

bool compare(student &s1, student &s2)
{
	
	if (s1.mKor == s2.mKor && s1.mEng == s2.mEng
		&& s1.mMath == s2.mMath)
		return s1.mName < s2.mName;
	if (s1.mKor == s2.mKor && s1.mEng == s2.mEng)
		return s1.mMath > s2.mMath;

	if (s1.mKor == s2.mKor)
		return s1.mEng < s2.mEng;

	return s1.mKor > s2.mKor;
}



int main()
{	
	int n;
	cin >> n;
	vector<student>v(n);

	for (int i = 0; i < n; i++)
	{	
		cin >> v[i].mName >> v[i].mKor >> v[i].mEng >> v[i].mMath;	
	}

	sort(v.begin(), v.end(), compare);

	for (const auto& i : v)
	{
		cout << i.mName << '\n';
	}

	return 0;
}

2. 클래스를 이용한 소스 코드

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

class student
{
public:

	student(string name, int kor, int eng, int math)
		: mName{ name }, mKor{ kor }, mEng{ eng }, mMath{ math }
	{}

	string mName;
	int mKor;
	int mEng;
	int mMath;

	bool operator < (student &other)
	{
		if (this->mKor == other.mKor && this->mEng == other.mEng
			&& this->mMath == other.mMath)
			return this->mName < other.mName;
		if (this->mKor == other.mKor && this->mEng == other.mEng)
			return this->mMath > other.mMath;

		if (this->mKor == other.mKor)
			return this->mEng < other.mEng;

		return this->mKor > other.mKor;
	}


};



int main()
{
	int n;
	cin >> n;
	vector<student>v;

	for (int i = 0; i < n; i++)
	{
		string name;
		int kor;
		int eng;
		int math;
		cin >> name >> kor >> eng >> math;
		v.emplace_back(name, kor, eng, math);
	}

	sort(v.begin(), v.end());
	for (const auto& i : v)
	{
		cout << i.mName << '\n';
	}

	return 0;
}
profile
🔥🔥🔥

0개의 댓글

관련 채용 정보