본문 바로가기

백준/C++

[C++] 백준 10825번 국영수

728x90

 

오늘은 학교 마지막 수업날 ㅎㅎ

수업이 끝나고 정말정말 기쁜 친구의 소식이 들려왔다 ㅎㅎ(정말정말 축하해~~~!!!)

 

그럼 오늘 문제도 파이팅~~!

 

문제

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

 

10825번: 국영수

첫째 줄에 도현이네 반의 학생의 수 N (1 ≤ N ≤ 100,000)이 주어진다. 둘째 줄부터 한 줄에 하나씩 각 학생의 이름, 국어, 영어, 수학 점수가 공백으로 구분해 주어진다. 점수는 1보다 크거나 같고, 1

www.acmicpc.net

 

 

구조체를 만들어서 입력받고 sort에서 조건에 따라 정렬하면 될 것 같다.

 

 

음...그런데 만들다보니까 어떻게 만들어야할지 잘 모르겠어서 다른 사람의 코드를 참고하여 내가 다시 작성해보았다...

 

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

typedef struct Student{
	string name;
	int kor;
	int eng;
	int math;
};

bool comp(Student a, Student b) {
	if (a.kor == b.kor&&a.eng == b.eng&&a.math == b.math) {//모든 점수가 같으면
		return a.name < b.name;
	}
	if (a.kor == b.kor&&a.eng == b.eng) {
		return a.math > b.math;
	}
	if (a.kor == b.kor) {
		return a.eng < b.eng;
	}
	return a.kor > b.kor;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	
	int n;
	cin >> n;
	vector <Student> student(n);
	for (int i = 0; i < n; i++) {
		cin >> student[i].name >> student[i].kor >> student[i].eng >> student[i].math;
	}
	sort(student.begin(), student.end(), comp);

	for (int i = 0; i < n; i++) {
		cout << student[i].name << "\n";
	}

	return 0;
}

 

휴...

 

[참고] https://code-kh-studio.tistory.com/63

 

뭔가 이렇게 보면 쉬운 것 같은데 막상 혼자하려니까 잘 되지는 않았다. 나중에 다시 한번 복습하며 풀어봐야겠다...

 

 

 

 

보니까 다른 사람들도 비슷하게 푸는 것 같다. 

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

struct student {
	string name;
	int ko;
	int en;
	int ma;
};

bool cmp(const student& s1, student& s2) {
	if (s1.ko == s2.ko) {
		if (s1.en == s2.en) {
			if (s1.ma == s2.ma) {
				return s1.name < s2.name;
			}
			return s1.ma > s2.ma;
		}
		return s1.en < s2.en;
	}
	return s1.ko > s2.ko;
}

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

	int n;
	cin >> n;
	student* st = new student[n];

	for (int i = 0; i < n; i++) {
		cin >> st[i].name >> st[i].ko >> st[i].en >> st[i].ma;
	}

	sort(st, st + n, cmp);

	for (int i = 0; i < n; i++) {
		cout << st[i].name << "\n";
	}
	delete[]st;
	return 0;
}
출처: https://plusratio.tistory.com/m/74

 

처음에 참고하여 풀었던 풀이는 벡터를 이용하였지만 여기서는 학생의 수를 입력받고 구조체를 동적으로 할당해주었다. 그리고 구조체를 정렬시킬 때도 조금 다르게 한 것을 볼 수 있었다.

 

어떤 사람들은 입력받은 구조체를 벡터 안에 넣고 정렬을 해주었다.

음.. 나는 vector를 많이 사용해서 그런지 처음에 참고해서 작성했던 방법이 더 편한 것 같다.

 

 

 

 

그럼 오늘은 이만...

728x90

'백준 > C++' 카테고리의 다른 글

[C++] 백준 1920번 수 찾기  (0) 2023.12.14
[C++] 백준 11399번 ATM  (2) 2023.12.13
[C++] 백준 11004번 k번째  (2) 2023.12.11
[C++] 백준 10816번 숫자 카드 2  (2) 2023.12.10
[C++] 백준 10989번 수 정렬하기 3  (4) 2023.12.09