강의

멘토링

로드맵

Inflearn Community Q&A

engus88274216's profile image
engus88274216

asked

Introduction to Java Algorithm Problem Solving: Coding Test Preparation

11. Choosing a Temporary Class President

코드 피드백 부탁 드립니다

Written on

·

291

0

import java.util.*;

public class Main {

    public int solution(int n, int[][] arr) {

        int answer = 0;

        int[] stu = new int[n];

        int cnt;

        int sum = 0;

        for (int i = 0; i < n; i++) {

            cnt = 0;

            for (int j = 0; j < 5; j++) {

                for (int k = 0; k < n; k++) {

                    if (i != j && arr[i][k] == arr[j][k]) {

                        cnt++;

                        stu[i] += cnt;

                        cnt = 0;

                    }

                }

            }

            sum = Math.max(sum, stu[i]);

            if (sum == stu[i]) {

                answer = i + 1;

            }

        }

        return answer;

    }

    public static void main(String[] args) {

        Main T = new Main();

        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();

        int[][] arr = new int[n][5];

        for (int i = 0; i < n; i++) {

            for (int j = 0; j < 5; j++) {

                arr[i][j] = sc.nextInt();

            }

        }

        System.out.print(T.solution(n, arr));

    }

}

java코테 준비 같이 해요!

Answer 1

0

codingcamp님의 프로필 이미지
codingcamp
Instructor

안녕하세요^^

첫 번째 줄에서 입력받는 n은 학생수입니다. 

위에 코드는 n=5일때만 통하는 코드입니다. 학생수를 3으로 해서 테스트 해보세요.

engus88274216's profile image
engus88274216

asked

Ask a question