코딩테스트 연습

코딩테스트 - 모의고사 (레벨 1)

고유빙글 2021. 5. 2. 10:13

문제는 이렇다.

 

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

1번 수포자가 찍는 방식: 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ...
2번 수포자가 찍는 방식: 2, 1, 2, 3, 2, 4, 2, 5, 2, 1, 2, 3, 2, 4, 2, 5, ...
3번 수포자가 찍는 방식: 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, ...

1번 문제부터 마지막 문제까지의 정답이 순서대로 들은 배열 answers가 주어졌을 때, 가장 많은 문제를 맞힌 사람이 누구인지 배열에 담아 return 하도록 solution 함수를 작성해주세요.

제한 조건

  • 시험은 최대 10,000 문제로 구성되어있습니다.
  • 문제의 정답은 1, 2, 3, 4, 5중 하나입니다.
  • 가장 높은 점수를 받은 사람이 여럿일 경우, return하는 값을 오름차순 정렬해주세요.

현재 풀고있는 코드는..

 

private static int[] solution(int[] answers) {
		
		
		int[] player1 = new int[answers.length];
		int[] player2 = new int[answers.length];
		int[] player3 = new int[answers.length];
		
		int j=1;
		for (int i = 0; i < answers.length; i++) {
			player1[i]=j;
			if(j==5) {
				j=1;
			}else {
				j++;
			}
		}
		
		j=0;
		int[] p2 = {2,1,2,3,2,4,2,5};
		for (int i = 0; i < answers.length; i++) {
			player2[i]=p2[j];
			if(j==7) {
				j=0;
			}else {
				j++;
			}
		}
		
		j=0;
		int[] p3 = {3,3,1,1,2,2,4,4,5,5};
		for (int i = 0; i < answers.length; i++) {
			player3[i]=p3[j];
			if(j==9) {
				j=0;
			}else {
				j++;
			}
		}
		
		int count1=0;
		int count2=0;
		int count3=0;
		for (int i = 0; i < answers.length; i++) {
			if(player1[i]==answers[i]) {
				count1++;
			}
			if(player2[i]==answers[i]) {
				count2++;
			}
			if(player3[i]==answers[i]) {
				count3++;
			}
		}
		
		ArrayList<Integer> counties = new ArrayList<Integer>();
		counties.add(count1);
		counties.add(count2);
		counties.add(count3);
		for (int i = 0; i < counties.size(); i++) {
			for (int k = 0; k < i; k++) {
				if(counties.get(i)>counties.get(k)) {
					int temp;
					temp = counties.get(i);
					counties.set(i, counties.get(k));
					counties.set(k, temp);
				}
			}
		}
		
		int check=0;
		if(counties.size()>=2) {
		while(counties.get(check).equals(counties.get(check+1))) {
			counties.remove(check+1);
		}
		}
		
		int[] answer = new int[counties.size()];
		
		for (int i = 0; i < answer.length; i++) {
			if(counties.get(i)==count1) {
				answer[i] = 1;
			}
			if(counties.get(i)==count2) {
				answer[i] = 2;
			}
			if(counties.get(i)==count3) {
				answer[i] = 3;
			}
			
		}
		
		for (int i = 0; i < answer.length; i++) {
			for (int k = 0; k < i; k++) {
				if(answer[i]<answer[k]) {
					int temp;
					temp = answer[i];
					answer[i] = answer[k];
					answer[k] = temp;
				}
			}
		}
		
		
		return answer;
	}

엄청..길다. 코드를 간단하게 작성하고 싶다. 우선 현재는 계속 outOfIndex 예외가 계속 발생하고 있다. 전혀 어딘지 감을 못 잡겠다. 잠을 좀 자야할것 같다.