알고리즘 풀이/프로그래머스

[level1] 프로그래머스 - 로또의 최고순위와 최저순위(JAVA)

데롱디롱 2021. 8. 25. 14:02
728x90
solution함수 안에 부분 보시면 됩니다!

 

- 1~45의 숫자 중, 로또 정답인 것은 true로 바꿔 준 후

  사용자가 입력한 lottos를 검사하면서, true인 수가 있었는지 수를 세면 된다.

 

- 이때 최소 맞은 개수는 0의 개수,

   최대 맞을 수 있는 개수는 0의 개수 + 이미 맞은 것의 개수

 

 

package com.pro.level1;

import java.util.Arrays;

public class 로또의최고순위와최저순위 {

	public static void main(String[] args) {
		int[] lottos = { 44, 1, 0, 0, 31, 25 };
		int[] win_nums = { 31, 10, 45, 1, 6, 19 };

		int answer[] = solution(lottos, win_nums);
		System.out.println(Arrays.toString(answer));
	}

	public static int[] solution(int[] lottos, int[] win_nums) {

		boolean num[] = new boolean[46];
		for (int i = 0; i < 6; i++)
			num[win_nums[i]] = true; // 로또 정답인 숫자 true

		int zero = 0; // 0개수
		int correct = 0; // 맞은 숫자개수
		for (int i = 0; i < 6; i++) {
			if (lottos[i] == 0)
				zero++;
			else if (num[lottos[i]])
				correct++;
		}

		int min = correct <= 1 ? 6 : 7 - correct;
		int max = (correct + zero) <= 1 ? 6 : 7 - (zero + correct);

		int[] answer = { max, min };
		return answer;
	}
}