문제
https://www.acmicpc.net/problem/2535
2535번: 아시아 정보올림피아드
첫 번째 줄에는 대회참가 학생 수를 나타내는 N이 주어진다. 단, 3 ≤ N ≤ 100이다. 두 번째 줄부터 N개의 줄에는 각 줄마다 한 학생의 소속 국가 번호, 학생 번호, 그리고 성적이 하나의 빈칸을 사
www.acmicpc.net
코드
#include <iostream>
#include <algorithm>
#include <vector>
#include <tuple>
using namespace std;
vector<tuple<int, int, int>> v;
bool compare(tuple<int,int,int>a,tuple<int,int,int>b){
return get<2>(a) > get<2>(b);
}
int main(){
int n;
cin >> n;
for(int i=0; i<n; i++){
int nationN, stuN, score;
cin >> nationN >> stuN >> score;
v.push_back(make_tuple(nationN, stuN, score));
}
sort(v.begin(), v.end(), compare);
for(int i=0; i<2; i++){
cout << get<0>(v[i]) << ' ' << get<1>(v[i]) << '\n';
}
if(get<0>(v[0])==get<0>(v[1])){
for(int i=2; i<n; i++){
if(get<0>(v[i])!=get<0>(v[0])){
cout << get<0>(v[i]) << ' ' << get<1>(v[i]) << '\n';
break;
}
}
}else{
cout << get<0>(v[2]) << ' ' << get<1>(v[2]) << '\n';
}
return 0;
}
정리
tuple을 사용하여 해결하였다.
tuple은 두 개 이상의 반환값이 있을 경우 사용하기 좋다.
이 문제에서는 세 개의 반환값이 필요하므로 tuple을 사용하였다.
튜플 선언
tuple<int, char, string> a<0, 'A', "ABCD">;
make_tuple(튜플 생성)
auto a = make_tuple(0, 'A', "ABCD");
get(튜플 원소 가져오기)
get<0>(a); // 0
get<1>(a); // A
get<2>(a); // ABCD
참조
'Beakjoon > else' 카테고리의 다른 글
[백준] 1094번 막대기 (C++) (0) | 2022.06.03 |
---|---|
[백준] 2605번 줄 세우기 (C++) - vector.insert() (0) | 2022.05.31 |
[백준] 9081번 단어 맞추기 (C++) (0) | 2022.05.19 |
[백준] 10973번 이전 순열 (C++) (0) | 2022.05.18 |
[백준] 10972번 다음 순열 (C++) (0) | 2022.05.18 |