본문 바로가기

BOJ/C++

BOJ [C++]) 10825번 국영수

728x90

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

 

10825번: 국영수

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

www.acmicpc.net

1. 문제

도현이네 반 학생 N명의 이름과 국어, 영어, 수학 점수가 주어진다. 이때, 다음과 같은 조건으로 학생의 성적을 정렬하는 프로그램을 작성하시오.

  1. 국어 점수가 감소하는 순서로
  2. 국어 점수가 같으면 영어 점수가 증가하는 순서로
  3. 국어 점수와 영어 점수가 같으면 수학 점수가 감소하는 순서로
  4. 모든 점수가 같으면 이름이 사전 순으로 증가하는 순서로 (단, 아스키 코드에서 대문자는 소문자보다 작으므로 사전순으로 앞에 온다.)

 

2. PS

- student class를 만들어 해결함.

- sort() 사용시 compare 함수를 만드는 것이 핵심.

- 클래스를 사용하지 않으면 튜플을 사용해도 될 것 같다.

#include <iostream>
#include <algorithm>
#include <vector>
#include <tuple>

using namespace std;

class student {
public:
    student(string _name, int score1, int score2, int score3) {
        name = _name;
        korean = score1;
        english = score2;
        math = score3;
    }
    string name;
    int korean;
    int english;
    int math;
};

bool compare(student a, student b){
    if(a.korean != b.korean) return a.korean > b.korean;
    else {
        if(a.english != b.english) return a.english < b.english;
        else {
            if(a.math != b.math) return a.math > b.math;
            else {
                return a.name < b.name;
            }
        }
    }
}

int main() {
    cin.tie(NULL);
    ios::sync_with_stdio(false);
    vector<student> vt;
    int n,korean,english,math;
    string name;
    cin >> n;
    
    for(int i = 0 ; i < n; i++) {
        cin >> name >> korean >> english >> math;
        student st(name,korean,english,math);
        vt.push_back(st);
    }
    stable_sort(vt.begin(), vt.end(), compare);
    
    for(int i = 0 ; i < vt.size(); i++) {
        cout << vt[i].name << '\n';
    }
}

 

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

BOJ [C++]) 10166번 관중석  (0) 2021.12.30
BOJ [C++]) 10819번 차이를 최대로  (0) 2021.12.27
BOJ [C++]) 10799번 쇠막대기  (0) 2021.12.23
BOJ [C++]) 11652번 카드  (0) 2021.12.23
BOJ [C++]) 1076번 저항  (0) 2021.12.21