Seren dev's blog
article thumbnail

https://school.programmers.co.kr/learn/courses/30/lessons/42840

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

풀이

시험 문제의 정답이 주어졌을 때, 1번, 2번, 3번 수포자의 점수를 구한 다음 높은 점수를 받은 수포자의 번호를 구해야 하며, 여러 명인 경우 오름차순으로 정렬하여 출력해야 한다.

 

1번, 2번, 3번 수포자의 점수를 구하는 로직은 따로 함수를 작성하여 구현하고, 구한 점수를 score 배열에 저장한다. 점수의 최댓값을 구한 후 그 최댓값을 가진 번호를 ArrayList에 저장한 다음 이 ArrayList를 int[] 배열로 변환한 후 그 int[] 배열을 리턴한다.

 

로직

1. 점수를 저장할 int[] score 배열을 선언하고 크기는 3만큼 할당한다.

2. mathOne(), mathTwo(), mathThree() 함수를 호출하여 각각 수포자 1번, 2번, 3번의 점수를 구한다.

3. score 배열값 중 최댓값을 max에 저장한다.

4. 가장 높은 점수를 가진 사람의 번호를 저장할 ArrayList<Integer> list를 선언한다.

5. score 배열을 탐색하여 score[i] == max라면 (i+1) 을 list에 저장한다.

6. list -> int[] 배열로 변환한 후 리턴한다.

코드

import java.util.*;

class Solution {
    
    public int[] solution(int[] answers) {
        
        int[] score = new int[3];
        
        score[0] = mathOne(answers);
        score[1] = mathTwo(answers);
        score[2] = mathThree(answers);
        
        int max = Math.max(score[0], score[1]);
        max = Math.max(max, score[2]);
        
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            if (score[i] == max)
                list.add(i+1);
        }

        int[] answer = list.stream().mapToInt(Integer::intValue).toArray();
        return answer;
    }
    
    public static int mathOne(int[] answers) {
        int cnt = 0;
        for (int i = 0; i < answers.length; i++) {
            if (i % 5 + 1 == answers[i])
                cnt++;
        }
        return cnt;
    }
    
    public static int mathTwo(int[] answers) {
        int cnt = 0;
        
        for (int i = 0; i < answers.length; i++) {
            if (i % 2 == 0 && answers[i] == 2)
                cnt++;
            else if (i % 8 == 1 && answers[i] == 1)
                cnt++;
            else if (i % 8 == 3 && answers[i] == 3)
                cnt++;
            else if (i % 8 == 5 && answers[i] == 4)
                cnt++;
            else if (i % 8 == 7 && answers[i] == 5)
                cnt++;  
        }
        return cnt;
    }
    
    public static int mathThree(int[] answers) {
        int cnt = 0;
        for (int i = 0; i < answers.length; i++) {
            int tmp = i % 10;
            if (tmp / 2 == 0 && answers[i] == 3) cnt++;
            else if (tmp / 2 == 1 && answers[i] == 1) cnt++;
            else if (tmp / 2 == 2 && answers[i] == 2) cnt++;
            else if (tmp / 2 == 3 && answers[i] == 4) cnt++;
            else if (tmp / 2 == 4 && answers[i] == 5) cnt++;
        }
        return cnt;
    }
}

 


다른 풀이 2

1번, 2번, 3번 수포자의 점수를 구하는 mathOne(), mathTwo(), mathThree() 함수를 다른 방식으로 구현할 수 있다.

-> if-else if문 을 사용하는 대신, 수포자가 찍는 방식을 미리 배열로 저장한 다음, 그 배열의 값과 정답을 비교한다.

import java.util.*;

class Solution {
    
    public int[] solution(int[] answers) {
        
        int[] score = new int[3];
        
        score[0] = mathOne(answers);
        score[1] = mathTwo(answers);
        score[2] = mathThree(answers);
        
        int max = Math.max(score[0], score[1]);
        max = Math.max(max, score[2]);
        
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            if (score[i] == max)
                list.add(i+1);
        }

        int[] answer = list.stream().mapToInt(Integer::intValue).toArray();
        return answer;
    }
    
    public static int mathOne(int[] answers) {
        int[] first = {1,2,3,4,5};
        
        int cnt = 0;
        for (int i = 0; i < answers.length; i++) {
            if (first[i%5] == answers[i])
                cnt++;
        }
        return cnt;
    }
    
    public static int mathTwo(int[] answers) {
        int[] second = {2, 1, 2, 3, 2, 4, 2, 5};
        int cnt = 0;
        
        for (int i = 0; i < answers.length; i++) {
            if (second[i%8] == answers[i])
                cnt++;  
        }
        return cnt;
    }
    
    public static int mathThree(int[] answers) {
        int[] third = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
        int cnt = 0;
        for (int i = 0; i < answers.length; i++) {
            if (third[i%10] == answers[i])
                cnt++;
        }
        return cnt;
    }
}

 

참고 : https://velog.io/@suzinxix/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4-%EB%AA%A8%EC%9D%98%EA%B3%A0%EC%82%AC

 

 


 

1번째 풀이
2번째 풀이

 

728x90
profile

Seren dev's blog

@Seren dev

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!