[알고리즘/백준] #6603 로또

JudyLia·2022년 2월 12일
0

알고리즘

목록 보기
39/61
post-thumbnail

난이도: Silver2
문제)독일 로또는 {1, 2, ..., 49}에서 수 6개를 고른다.

로또 번호를 선택하는데 사용되는 가장 유명한 전략은 49가지 수 중 k(k>6)개의 수를 골라 집합 S를 만든 다음 그 수만 가지고 번호를 선택하는 것이다.

예를 들어, k=8, S={1,2,3,5,8,13,21,34}인 경우 이 집합 S에서 수를 고를 수 있는 경우의 수는 총 28가지이다. ([1,2,3,5,8,13], [1,2,3,5,8,21], [1,2,3,5,8,34], [1,2,3,5,13,21], ..., [3,5,8,13,21,34])

집합 S와 k가 주어졌을 때, 수를 고르는 모든 방법을 구하는 프로그램을 작성하시오.

package algorithm_study.day0214;

import java.util.Arrays;
import java.util.Scanner;

public class BJ_6603 {
	static int[] arr,answer;
	static int k;
	static StringBuilder sb = new StringBuilder();
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		while(true) {
			k=sc.nextInt();
			if(k==0) break;
			arr=new int[k];
			answer = new int[6];
			for(int i=0;i<k;i++) {
				arr[i]=sc.nextInt();
			}
			comb(0,0);
			sb.append("\n");
		}
		
		System.out.print(sb.toString());
	}
	
	public static void comb(int cnt, int start) {
		if(cnt==6) {
			for(int i=0;i<6;i++) sb.append(answer[i]).append(" ");
			sb.append("\n");
			return;
		}
		
		for(int i=start;i<k;i++) {
			answer[cnt]=arr[i];
			comb(cnt+1,i+1);
		}
	}
}
profile
안녕:)

0개의 댓글

관련 채용 정보