https://www.acmicpc.net/problem/11637
11637번: 인기 투표
각 테스트 케이스는 첫 번째 줄부터 순서대로 출력된다. 최다 득표자가 과반수 득표를 했을경우에는 "majority winner R", 절반 이하의 득표를 하였을 경우엔 "minority winner R"가 되며, 최다 득표자가 없
www.acmicpc.net
알고리즘 분류
- 난이도 : 실버5
- 구현 언어 : 자바
- 소요시간 : 1시간
- 코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Vote_11637 {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int testCnt = Integer.parseInt(bf.readLine()); //총 test 횟수 입력
List<String> textList = new ArrayList<String>(); // 출력 값 담을 상자
for (int i = 0; i < testCnt; i++) {
int candidateCount = Integer.parseInt(bf.readLine()); // 후보자 수
List<Integer> voteCntList = new ArrayList<Integer>(); //투표함
int voteTotalCnt = 0;
int voteMajority = 0;
for (int j = 0; j < candidateCount; j++) {
int voteCnt = Integer.parseInt(bf.readLine());
voteCntList.add(voteCnt);
voteTotalCnt += voteCnt;
}
voteMajority = voteTotalCnt/2 + 1; // 과반수
int topVote = 0;
int who = 0;
int same = 0;
for (int j = 0; j < voteCntList.size(); j++) { //최다 득표자 설정
if(voteCntList.get(j) > topVote) {
topVote = voteCntList.get(j);
who = j + 1;
same = 0;
}else if(voteCntList.get(j) == topVote) {
same += 1;
}
}
String text = ""; // 조건별 출력 값 설정
if(same != 0) {
text = "no winner";
}else {
if(voteMajority <= voteCntList.get(who-1)) {
text = "majority winner " + who;
}else {
text = "minority winner " + who;
}
}
textList.add(text);
}
for (String string : textList) {
System.out.println(string);
}
}
}
출처
ICPC > Regionals > North America > Rocky Mountain Regional > 2015 Rocky Mountain Regional Contest A번
- 문제의 오타를 찾은 사람: corea
- 잘못된 번역을 찾은 사람: psychobabo
- 어색한 표현을 찾은 사람: solarmagic
- 문제를 번역한 사람: vumbumy
'프로그래밍 > Algorithm' 카테고리의 다른 글
[백준 1874] 스택수열 - 자바 (0) | 2022.05.26 |
---|---|
[백준 11866] 요세푸스 문제 0 - 자바 (0) | 2022.05.23 |
[백준 10828] 스택 - 자바 (0) | 2022.05.23 |
[백준 11721] 열 개씩 끊어 출력하기 - 자바 (0) | 2022.05.23 |
[백준 2523] 별 찍기 - 13 - 자바 (0) | 2022.05.23 |