CS/Algorithm
모의고사
Hinos
2021. 5. 4. 13:48
반응형
https://programmers.co.kr/learn/courses/30/lessons/42840
코딩테스트 연습 - 모의고사
수포자는 수학을 포기한 사람의 준말입니다. 수포자 삼인방은 모의고사에 수학 문제를 전부 찍으려 합니다. 수포자는 1번 문제부터 마지막 문제까지 다음과 같이 찍습니다. 1번 수포자가 찍는
programmers.co.kr
문제가 쉬운편이라 금방 풀었다~
효울성도 쏘쏘
package question;
import java.util.ArrayList;
import java.util.Collections;
public class 모의고사
{
public static void main(String [] args)
{
int[] answers = {1, 3, 2, 4, 2};
int [] result = solution(answers);
System.out.println(result);
}
public static int[] solution(int[] answers)
{
int[] firstPattern = {1, 2, 3, 4, 5};
int[] secondPattern = {2, 1, 2, 3, 2, 4, 2, 5};
int[] thirdPattern = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
int firstCount = 0;
int secondCount = 0;
int thirdCount = 0;
for(int i = 0; i < answers.length; i++)
{
if (firstPattern[i % firstPattern.length] == answers[i])
{
firstCount++;
}
if (secondPattern[i % secondPattern.length] == answers[i])
{
secondCount++;
}
if (thirdPattern[i % thirdPattern.length] == answers[i])
{
thirdCount++;
}
}
int max = 0;
if (firstCount >= secondCount && firstCount >= thirdCount)
{
max = firstCount;
}
if (secondCount >= firstCount && secondCount >= thirdCount)
{
max = secondCount;
}
if (thirdCount >= firstCount && thirdCount >= secondCount)
{
max = thirdCount;
}
ArrayList<Integer> answer = new ArrayList<Integer>();
if (max == firstCount)
{
answer.add(1);
}
if (max == secondCount)
{
answer.add(2);
}
if (max == thirdCount)
{
answer.add(3);
}
Collections.sort(answer);
int [] result = new int[answer.size()];
for (int i = 0; i < answer.size(); i++)
{
result[i] = answer.get(i);
}
return result;
}
}
반응형