개발/코딩

[프로그래밍] 프로그래머스 예제문제 풀어보기 <모의고사> 성공

mabb 2022. 5. 27. 17:36
반응형
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.util.*;
 
class Solution {
    public int[] solution(int[] answers) {
        
        int[] soopo1 = {1,2,3,4,5};
        int[] soopo2 = {2,1,2,3,2,4,2,5};
        int[] soopo3 = {3,3,1,1,2,2,4,4,5,5};
        
        int indexTemp1 = 5;
        int indexTemp2 = 8;
        int indexTemp3 = 10;
        
        int succ1 = 0;
        int succ2 = 0;
        int succ3 = 0;
        
        for(int j = 0 ; j < answers.length ; j ++){
            int indexTrans1 = (j+indexTemp1) % indexTemp1;    
            int indexTrans2 = (j+indexTemp2) % indexTemp2;  
            int indexTrans3 = (j+indexTemp3) % indexTemp3;  
            
            if(answers[j] == soopo1[indexTrans1]){
                succ1 ++;
            }
            if(answers[j] == soopo2[indexTrans2]){
                succ2 ++;
            }
            if(answers[j] == soopo3[indexTrans3]){
                succ3 ++;
            } 
        }        
        
        
        int max = succ1;
        max = (max>succ2) ? max:succ2 ;
        max = (max>succ3) ? max:succ3 ;
        
        
        ArrayList<Integer> answerTemp = new ArrayList<>();
        int[] succArray = new int[]{succ1,succ2,succ3};
        for(int i = 0; i < 3 ; i++) {             
            if (succArray[i] == max){
                 answerTemp.add(i+1);
            }         
        }
 
 
        int[] answer = new int[answerTemp.size()];
        for(int k =0 ; k < answer.length ; k++){
            answer[k] = answerTemp.get(k);
        }
        
        for(int h = 0 ; h < answer.length ; h ++){
            System.out.println(answer[h]);
        }
              
        return answer;
    }
}
cs

 

 

 

 

반응형