카테고리 없음

백준 30999 자바(Java) 민주주의 - 과반수

서병렬 2025. 3. 7. 20:36

태그

  • 과반수(모듈로, ceil)

과반수 개념을 코드에 적용하고자 하면 아래의 2가지의 방법이 있다.

n >= (N-1) / 2 + 1 // 모듈로
n >= (int)Math.ceil(N) // ceil함수
static void solve() throws Exception {
	int prob = scan.nextInt();
	int professor = scan.nextInt();
	int exam = 0;
	for (int i = 0; i < prob; i++) {
		String line = scan.nextLine();
		int oCount = 0;
		for (int j = 0; j < professor; j++) {
			oCount += line.charAt(j) == 'O' ? 1 : 0;
		}
		if(oCount >= (professor-1)/2 + 1){
			exam++;
		}
	}
	System.out.println(exam);
}