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

SmileJun·2025년 2월 26일

알고리즘

목록 보기
6/34

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

C++

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

struct Student { // 구조체
    string name;
    int word;
    int eng;
    int math;
};

bool compare(Student a, Student b) {
    if(a.word == b.word && a.eng == b.eng && a.math == b.math) {
        return a.name < b.name;
    }
    else if(a.word == b.word && a.eng == b.eng) {
        return a.math > b.math;
    }
    else if(a.word == b.word) {
        return a.eng < b.eng;
    }
    else {
        return a.word > b.word;
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n = 0;
    cin >> n;
    vector<Student> v(n);
    for(int i = 0; i < n; i++) {
        cin >> v[i].name >> v[i].word >> v[i].eng >> v[i].math;
    }

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

    for(int j = 0; j < n; j++) {
        cout << v[j].name << "\n";
    }
    return 0;
}

문제풀이

  • 하나의 그릇에 이름, 국어, 영어, 수학 4가지의 정보들이 담겨있어야 한다. 따라서 모든 정보를 하나로 묶을 수 있는 구조체를 사용했다. 구조체와 벡터를 사용해서 각각의 이름과 점수들을 입력받았다. 그 다음 정렬 조건에 따라 별도로 compare 함수를 만들었다.

Comment

  • 구조체의 개념을 정확히 몰라서, 어떻게 4가지의 정보를 한꺼번에 담을 수 있을까에 대해 고민했다. 그래서 이 문제는 해당 블로그를 참고해서 문제를 해결했다.

참고블로그 : https://lake0989.tistory.com/164

profile
하루하루는 성실하게, 인생 전체는 되는대로

0개의 댓글