알고리즘 분류

  • 난이도 : 실버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

+ Recent posts