코테/lvl1

프로그래머스 모의고사(lvl1) 풀어보기

디비드킴 2026. 9. 7. 18:45

문제링크

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

 

프로그래머스

SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 


문제 접근법

-1번,2번,3번 각각 수험자가 찍는 방식이랑 입력된 정답배열 비교해서

최다 득점자를 배열에 담아서 알려주기


문제 풀이법

1,2,3번 패턴자 정답 패턴과 배열은 전부 비교하면 되는 간단한 방식


코드

public static List<Integer> solution(int[] answers) {
        List<Integer> answer =  new ArrayList<>();
        int[] f = {1,2,3,4,5};
        int[] s = {2, 1, 2, 3, 2, 4, 2, 5};
        int[] t = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
        int findex=0;
        int sindex=0;
         int tindex=0;
         int fscore=0;
         int sscore=0;
         int tscore=0;
         int fcount=0;
         int scount=0;
         int tcount=0;
        for(int i=0;i<answers.length;i++){
          if(fcount>f.length-1){
            findex=0;
            fcount=0;
          }
          if(f[findex]==answers[i]){
            fscore+=1;
          }
          if(scount>s.length-1){
            sindex=0;
            scount=0;
          }
          if(s[sindex]==answers[i]){
            sscore+=1;
          }
           if(tcount>t.length-1){
            tindex=0;
            tcount=0;
          }
          if(t[tindex]==answers[i]){
            tscore+=1;
          }
          findex+=1;
          fcount+=1;
          sindex+=1;
          scount+=1;
          tindex+=1;
          tcount+=1;
        }
        int max=Math.max(Math.max(fscore, sscore), tscore);
        if(max<=fscore){
          answer.add(1);
        }
        if(max<=sscore){
          answer.add(2);
        }
        if(max<=tscore){
          answer.add(3);
        }
        return answer;
    }

결과


gpt 피드백

 

필요없는 변수너무많다 

count,index동일 기능이므로 index하나만 쓰셈


배운점

gpt 피드백 대로 사실 처음엔 
나도 count변수를 만들지 않았으나
정답배열이 길어졌을때를 생각못하고 초기화 부분에서
계속 1이 되는 버그가 발생해서 그냥 쉽게 카운트하나더만들자
했는데 다시 한번 gpt코드와 비교후 어케 index하나로 처리했는지 인지
해야겠음
 
gpt 코드 
import java.util.*;
 
public static int[] solution(int[] answers) {
 
    int[] f = {1, 2, 3, 4, 5};
    int[] s = {2, 1, 2, 3, 2, 4, 2, 5};
    int[] t = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
 
    int findex = 0;
    int sindex = 0;
    int tindex = 0;
 
    int fscore = 0;
    int sscore = 0;
    int tscore = 0;
 
    for (int i = 0; i < answers.length; i++) {
 
        if (findex >= f.length) {
            findex = 0;
        }
 
        if (sindex >= s.length) {
            sindex = 0;
        }
 
        if (tindex >= t.length) {
            tindex = 0;
        }
 
        if (f[findex] == answers[i]) {
            fscore++;
        }
 
        if (s[sindex] == answers[i]) {
            sscore++;
        }
 
        if (t[tindex] == answers[i]) {
            tscore++;
        }
 
        findex++;
        sindex++;
        tindex++;
    }
 
    int max = Math.max(Math.max(fscore, sscore), tscore);
 
    List<Integer> list = new ArrayList<>();
 
    if (fscore == max) {
        list.add(1);
    }
 
    if (sscore == max) {
        list.add(2);
    }
 
    if (tscore == max) {
        list.add(3);
    }
 
    int[] answer = new int[list.size()];
 
    for (int i = 0; i < list.size(); i++) {
        answer[i] = list.get(i);
    }
 
    return answer;
}