import java.util.ArrayList;
// 모의고사 - 완전탐색 ( brute force )
public class Trial_exam {
// public int[] solution(int[] answers) {
//
// int[] answer = {};
// int[][] supo = { { 1, 2, 3, 4, 5 }, { 2, 1, 2, 3, 2, 4, 2, 5 }, { 3, 3, 1, 1, 2, 2, 4, 4, 5, 5 } };
// int temp[] = new int[supo.length];
//
// int max = 0, c = 0, z = 0;
// int i, count, answercount;
//
// for (int j = 0; j < supo.length; j++) {
//
// ArrayList<Integer> list = new ArrayList<Integer>();
// count = answercount = i = 0;
//
// while (true) {
// list.add(supo[j][i]);
// i++;
// count++;
// if (i == supo[j].length) {
// i = 0;
// }
// if (count == answers.length) {
// break;
// }
// }
//
// for (int k = 0; k < list.size(); k++) {
//
// if (list.get(k) == answers[k]) {
// answercount++;
// }
//
// }
// temp[j] = answercount;
// }
//
// for (int t = 0; t < temp.length; t++) {
//
// if (max < temp[t]) {
// max = temp[t];
// }
// }
//
// for (int h = 0; h < temp.length; h++) {
//
// if (max == temp[h]) {
// c++;
// }
//
// }
// answer = new int[c];
// for (int h = 0; h < temp.length; h++) {
//
// if (max == temp[h]) {
// answer[z] = h + 1;
// z++;
// if (z == c) {
// break;
// }
// }
//
// }
//
// return answer;
// }
public int[] solution(int[] answers) {
int[] supo1 = { 1, 2, 3, 4, 5 };
int[] supo2 = { 2, 1, 2, 3, 2, 4, 2, 5 };
int[] supo3 = { 3, 3, 1, 1, 2, 2, 4, 4, 5, 5 };
int[] score = new int[3]; // 맞춘 개수를 카운트할 score 배열
for (int i = 0; i < answers.length; i++) {
if (answers[i] == supo1[i % supo1.length]) { // 반복되는 패턴을 Mod 함수를 통해 간단하게 작성한다.
score[0]++;
}
if (answers[i] == supo2[i % supo2.length]) {
score[1]++;
}
if (answers[i] == supo3[i % supo3.length]) {
score[2]++;
}
}
int max = Math.max(score[0], Math.max(score[1], score[2])); // Math.max() 함수 중첩으로 max를 구함
ArrayList<Integer> list = new ArrayList<Integer>();
if (max == score[0]) {
list.add(1);
}
if (max == score[1]) { // else if 문으로 하면 답 안나옴. 주의
list.add(2);
}
if (max == score[2]) {
list.add(3);
}
return list.stream().mapToInt(i -> i.intValue()).toArray(); // list -> array
}
public static void main(String[] args) {
Trial_exam t = new Trial_exam();
// int arr[] = { 1, 2, 3, 4, 5 };
// int arr1[] = { 1, 3, 2, 4, 2 };
int arr2[] = { 2, 1, 2 };
// int arr3[] = { 4, 4, 4, 5, 3 };
int[] a = t.solution(arr2);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + ", ");
}
}
}
int[] arr = new int[n]; - int 형 배열은 선언시 각 원소는 0으로 자동 초기화 됨
list.stream().mapToInt(i -> i.intValue()).toArray(); // list -> array 변환
set.stream().mapToInt(i -> i.intValue()).toArray(); // set -> array 변환 (Set도 가능)