문제 설명
1번부터 3번 수포자까지 찍는 방식이 있습니다.
가장 많은 문제를 맞힌 사람을 반환하는 문제입니다.
공동 1등일 경우 번호 오름차순으로 반환해야 합니다.
문제 풀이
학생이 찍는 방법이 모두 다르기 때문에 Student 클래스를 상속받은 학생 3명의 클래스를 생성합니다.
(각 Student 클래스는 제출 코드에서)
public class Student
{
public int currentAnswer;
public int answerCount;
public void CheckAnswer(int answer)
{
if (currentAnswer == answer)
{
answerCount++;
}
NextAnswer();
}
public virtual void NextAnswer() {}
}
현재 정답(currentAnswer)과 NextAnswer()를 찍는 방식에 맞게 설정해줍니다.
answers와 정답 비교 후 각 학생들의 점수를 비교해서 1등 또는 동점자를 반환합니다.
제출 코드
using System;
using System.Collections;
using System.Collections.Generic;
public class Solution
{
public int[] solution(int[] answers)
{
List<int> answer = new List<int>();
List<Student> students = new List<Student>();
students.Add(new Student1());
students.Add(new Student2());
students.Add(new Student3());
for (int i = 0; i < answers.Length; i++)
{
for (int j = 0; j < students.Count; j++)
{
students[j].CheckAnswer(answers[i]);
}
}
int max = -1;
for (int i = 0; i < students.Count; i++)
{
if (students[i].answerCount > max)
{
answer.Clear();
answer.Add(i + 1);
max = students[i].answerCount;
}
else if (students[i].answerCount == max)
{
answer.Add(i + 1);
}
}
return answer.ToArray();
}
}
public class Student
{
public int currentAnswer;
public int answerCount;
public void CheckAnswer(int answer)
{
if (currentAnswer == answer)
{
answerCount++;
}
NextAnswer();
}
public virtual void NextAnswer() {}
}
public class Student1 : Student
{
public Student1()
{
currentAnswer = 1;
}
public override void NextAnswer()
{
currentAnswer = currentAnswer + 1 > 5 ? 1 : currentAnswer + 1;
}
}
public class Student2 : Student
{
int stepNumber;
public Student2()
{
currentAnswer = 2;
stepNumber = 5;
}
public override void NextAnswer()
{
if (currentAnswer == 2)
{
if (stepNumber == 5)
stepNumber = 1;
else if (stepNumber == 1)
stepNumber = 3;
else if (stepNumber == 3)
stepNumber = 4;
else if (stepNumber == 4)
stepNumber = 5;
currentAnswer = stepNumber;
}
else
{
currentAnswer = 2;
}
}
}
public class Student3 : Student
{
int beforeAnswer;
public Student3()
{
currentAnswer = 3;
}
public override void NextAnswer()
{
int temp = currentAnswer;
if (beforeAnswer == currentAnswer)
{
if (currentAnswer == 3)
currentAnswer = 1;
else if (currentAnswer == 1)
currentAnswer = 2;
else if (currentAnswer == 2)
currentAnswer = 4;
else if (currentAnswer == 4)
currentAnswer = 5;
else if (currentAnswer == 5)
currentAnswer = 3;
}
beforeAnswer = temp;
}
}