본문 바로가기
알고리즘/Array

모의고사

by e-pd 2020. 8. 3.

 

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

 

코딩테스트 연습 - 모의고사

수포자는 수학을 포기한 사람의 준말입니다. 수포자 삼인방은 모의고사에 수학 문제를 전부 찍으려 합니다. 수포자는 1번 문제부터 마지막 문제까지 다음과 같이 찍습니다. 1번 수포자가 찍는 ��

programmers.co.kr

1.  정답 배열이 들어오기때문에 사용자의 입력리스트로 나눠서 일치여부를 합산하면 된다.

2. 사용자의 구분과 배열 정보를 갖고 있어야하기때문에 class로 사용자구분, 정답지, 맞은 갯수를 갖게한다. 

 

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
61
62
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
 
class Student {
    private int id;
    private int match;
    private int[] submitAnswers = {};
 
    public Student(int id, int[] submitAnswers, int[] answers) {
        this.id = id;
        this.submitAnswers = submitAnswers;
        this.match = checkMatches(answers);
    }
 
    private int checkMatches(int[] answers) {
        int temp = 0;
        for (int i = 0; i < answers.length; i++) {
            if (answers[i] == submitAnswers[i % submitAnswers.length]) {
                temp++;
            }
        }
        return temp;
    }
 
    public int getId() {
        return id;
    }
 
    public int getMatch() {
        return match;
    }
}
 
 
class Solution {
    private static int[] student1Submits = {12345};
    private static int[] student2Submits = {21232425};
    private static int[] student3Submits = {3311224455};
 
    public int[] solution(int[] answers) {
        List<Integer> answer = new ArrayList<>();
 
        List<Student> students = Arrays.asList(
                new Student(1, student1Submits, answers),
                new Student(2, student2Submits, answers),
                new Student(3, student3Submits, answers)
        );
 
        int max = students.stream()
                .map(Student::getMatch)
                .max(Integer::compare)
                .get();
 
        return students.stream()
                .filter(student -> student.getMatch() == max)
                .map(Student::getId)
                .mapToInt(Integer::intValue)
                .toArray();
    }
}
cs

 

'알고리즘 > Array' 카테고리의 다른 글

구명보트  (0) 2020.09.05
문자열 내 마음대로 정렬하기  (0) 2020.08.31
다음큰 숫자  (0) 2020.08.30
주식가격  (0) 2020.08.22
하샤드 수 구하기  (0) 2020.08.03