[백준 2456] 나는 학급회장이다 (C#, C++) - soo:bak
작성일 :
문제 링크
설명
각 후보에게 부여된 점수를 바탕으로 총합과 우선 점수 개수 비교를 통해 회장을 결정하는 구현 문제입니다.
- 각 학생은 세 명의 후보에게 각각 1, 2, 3점 중 하나를 부여합니다.
- 각 후보의 총점을 계산하고, 가장 높은 총점을 받은 후보가 회장이 됩니다.
- 동점일 경우
3점
이 더 많은 후보가 우선이며,
그래도 같다면2점
이 더 많은 후보가 우선입니다. - 모두 같다면 무승부로 간주하고
0
을 출력합니다.
접근법
- 세 후보 각각에 대해 총점, 3점 횟수, 2점 횟수를 별도로 저장합니다.
- 총점이 높은 후보를 우선 선택하고, 동점이면 3점 수, 그 다음 2점 수 순으로 우선순위를 결정합니다.
- 최종 결과는 회장 번호(1~3)와 해당 총점을 출력하며, 무승부 시에는
0
과 점수를 출력합니다.
Code
[ C# ]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System;
using System.Linq;
class Program {
static void Main() {
int n = int.Parse(Console.ReadLine());
int[] total = new int[3], threes = new int[3], twos = new int[3];
for (int i = 0; i < n; i++) {
var line = Console.ReadLine().Split().Select(int.Parse).ToArray();
for (int j = 0; j < 3; j++) {
total[j] += line[j];
if (line[j] == 3) threes[j]++;
else if (line[j] == 2) twos[j]++;
}
}
int winner = 0;
bool isDraw = false;
for (int i = 1; i < 3; i++) {
if (total[i] > total[winner]) {
winner = i;
isDraw = false;
} else if (total[i] == total[winner]) {
if (threes[i] > threes[winner]) {
winner = i;
isDraw = false;
} else if (threes[i] == threes[winner]) {
if (twos[i] > twos[winner]) {
winner = i;
isDraw = false;
} else if (twos[i] == twos[winner]) {
isDraw = true;
}
}
}
}
if (isDraw)
Console.WriteLine($"0 {total[winner]}");
else
Console.WriteLine($"{winner + 1} {total[winner]}");
}
}
[ C++ ]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n; cin >> n;
int total[3] = {0, }, threes[3] = {0, }, twos[3] = {0, };
for (int i = 0; i < n; i++) {
for (int j = 0; j < 3; j++) {
int score; cin >> score;
total[j] += score;
if (score == 3) threes[j]++;
else if (score == 2) twos[j]++;
}
}
int winner = 0;
bool isDraw = false;
for (int i = 1; i < 3; i++) {
if (total[i] > total[winner]) {
winner = i;
isDraw = false;
} else if (total[i] == total[winner]) {
if (threes[i] > threes[winner]) {
winner = i;
isDraw = false;
} else if (threes[i] == threes[winner]) {
if (twos[i] > twos[winner]) {
winner = i;
isDraw = false;
} else if (twos[i] == twos[winner])
isDraw = true;
}
}
}
if (isDraw) cout << "0 " << total[winner] << "\n";
else cout << winner + 1 << " " << total[winner] << "\n";
return 0;
}