안녕하세요.
여행벌입니다.
단순 구현 문제지만 깔끔하게 구현하고 싶어서 노력을 많이 했지만 실패한 문제입니다.
https://www.acmicpc.net/problem/2456
2456번: 나는 학급회장이다
첫째 줄에는 반의 학생들의 수 N (3<=N<=1,000)이 주어진다. 다음 N개의 각 줄에는 각 학생이 제출한 회장후보 3명에 대한 선호 점수가 주어지는 데, 첫 번째 점수는 후보 1번에 대한 점수이고 두 번째 점수는 후보 2번에 대한 점수이고 세 번째 점수는 후보 3번에 대한 점수이다. 이 세 점수는 서로 다르며, 1, 2, 3이 정확히 한 번씩 나타난다.
www.acmicpc.net
[알고리즘설계]
문제를 읽고 정말 단순히 구현합니다.
#include<iostream>
#include<algorithm>
using namespace std;
int ninput, total_score[4][3] = {};
bool cmp(int x, int y) {
if (total_score[0][x] < total_score[0][y])
return true;
else if (total_score[0][x] > total_score[0][y])
return false;
else {
if (total_score[1][x] < total_score[1][y])
return true;
else if (total_score[1][x] > total_score[1][y])
return false;
else {
if (total_score[2][x] < total_score[2][y])
return true;
else
return false;
}
}
}
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> ninput;
for (int i = 0; i < ninput; i++) {
int input[3];
for (int j = 0; j < 3; j++) {
cin >> input[j];
total_score[0][j] += input[j];
total_score[4 - input[j]][j]++;
}
}
int answer[3] = { 0,1,2 };
sort(answer, answer + 3, cmp);
if ( total_score[0][answer[1]] == total_score[0][answer[2]] &&
total_score[1][answer[1]] == total_score[1][answer[2]] &&
total_score[2][answer[1]] == total_score[2][answer[2]])
cout << 0 << ' ' << total_score[0][answer[2]];
else
cout << answer[2]+1 << ' ' << total_score[0][answer[2]];
}
열심히 공부하고 노력하고 있는 꿈 많은 예비 개발자입니다.
혼자서 공부한 내용을 정리하고 포스팅하다 보니 틀린 내용이 있을 수도 있습니다.
많은 조언과 가르침 주실 분은 댓글로 자유롭게 남겨주셔도 좋을 것 같습니다!
감사합니다.
'Problem Solving > BOJ' 카테고리의 다른 글
[BOJ] 1101 - Fly me to the Alpha Centauri (0) | 2019.08.21 |
---|---|
[BOJ] 1436 - 영화감독 숌 (0) | 2019.08.21 |
[BOJ] 2579 - 계단 오르기 (0) | 2019.08.21 |
[BOJ] 6603 - LOTTO (0) | 2019.08.21 |
[BOJ] 2668 - 숫자고르기 (0) | 2019.08.16 |