※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※

 

이번에 볼 문제는 백준 16175번 문제인 General Election이다.
문제는 아래 링크를 확인하자.

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

 

16175번: General Election

General Election is over, now it is time to count the votes! There are N (2 <= N <= 5) candidates and M (1 <= M <= 100) vote regions. Given the number of votes for each candidate from each region, determine which candidate is the winner (one with t

www.acmicpc.net

주어지는 지역별 각 후보의 득표를 더해 가장 많은 표를 구한 사람을 찾는 문제이다.

 

이는 반복문과 배열을 이용해 아래와 같이 간단히 구현 및 해결할 수 있다.

 

아래는 제출한 소스코드이다.

#include <iostream>
#include <cstring>
using namespace std;

int arr[5];

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	int T; cin >> T;
	while (T--) {
		memset(arr, 0, sizeof(arr));
		int N, M; cin >> N >> M;
		for (int i = 0; i < M; i++) {
			for (int j = 0; j < N; j++) {
				int x; cin >> x;
				arr[j] += x;
			}
		}
		int mx = 0, idx = 0;
		for (int i = 0; i < N; i++) {
			if (mx < arr[i]) {
				mx = arr[i], idx = i;
			}
		}

		cout << idx + 1 << '\n';
	}
}
728x90

'BOJ' 카테고리의 다른 글

[BOJ 13223 // C++] 소금 폭탄  (0) 2022.11.25
[BOJ 13224 // C++] Chop Cup  (0) 2022.11.25
[BOJ 15239 // C++] Password check  (0) 2022.11.24
[BOJ 13240 // C++] Chessboard  (0) 2022.11.24
[BOJ 20017 // C++] Топот котов  (0) 2022.11.24

+ Recent posts