[알고리즘]/알고리즘

[알고리즘] 백준 1268번 : 임시 반장 정하기 (브론즈1)

쿠릉쿠릉 쾅쾅 2022. 4. 18. 15:34
728x90

 

 

문제

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

 

📌 알고리즘 풀이

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class Main {
	
	
	public static void main(String[] args) throws IOException {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		// 총 학생 수
		int allStudents = Integer.parseInt(br.readLine()); 
		
		int allGrades = 5;
		
		// int[학생수+1][학년수+1] (index는 0부터 시작하므로 1을 더해줌)
		int[][] arr = new int[allStudents+1][allGrades+1];
		
		// 입력 값 받기
		for(int i=1; i<=allStudents; i++) {
			String[] strArr = br.readLine().split(" ");
			
			for(int k=1; k<=allGrades; k++) {
				arr[i][k] = Integer.parseInt(strArr[k-1]);
			}
		}
		
		int classPresident = 0;
		int max = Integer.MIN_VALUE;
		int count = 0;
		
		for(int i=1; i<=allStudents; i++) {  // 기준 학생
			count = 0;
			for(int k=1; k<=allStudents; k++) { // 비교 대상 학생
				if(i==k) continue;
				
				for(int j=1; j<=allGrades; j++) { 
					if(arr[i][j] == arr[k][j]) {
						count++;
						break;
					}
				}
			}
			if(count>max) {
				max = count;
				classPresident = i;
			}
		}  // end for i

		System.out.println(classPresident);
		
	}


}

 

 

 

728x90