코딩테스트/백준

백준 6603 : 로또 _자바 Java

플래시🦥 2023. 1. 28.
반응형

https://www.acmicpc.net/problem/6603

 

6603번: 로또

입력은 여러 개의 테스트 케이스로 이루어져 있다. 각 테스트 케이스는 한 줄로 이루어져 있다. 첫 번째 수는 k (6 < k < 13)이고, 다음 k개 수는 집합 S에 포함되는 수이다. S의 원소는 오름차순으로

www.acmicpc.net


입력이 7 1 2 3 4 5 6 7 일때,

개수(K) : 7 , 배열(num[]) : 1 2 3 4 5 6 7

f(dp,start)

f(0,0) -> i=0, check[0] = true;  f(1,1);

f(1,1) -> i=1, check[1] = true;  f(2,2);

f(2,2) -> i=2, check[2] = true;  f(3,3);

f(3,3) -> i=3, check[3] = true;  f(4,4);

f(4,4) -> i=4, check[4] = true;  f(5,5);

f(5,5) -> i=5, check[5] = true;  f(6,6);

f(6,6) -> check={1,1,1,1,1,1,0}, true이면 num[i]출력 =>1 2 3 4 5 6 \n  f(6,6)종료, f(5,5)로 돌아감.

f(5,5) -> i=5, check[5]=false;

              i=6, check[6]=true; f(6,7); 

f(6,7) -> check={1,1,1,1,1,0,1},  true이면 num[i]출력 =>1 2 3 4 5 7 \n  f(6,7)종료, f(5,5)로 돌아감.

f(5,5) -> i=6, check[6]=false;  f(5,5) 종료, f(4,4)로 돌아감.

f(4,4) -> i=4, check[4] = false;

              i=5,  check[5]= true; f(5,6);

f(5,6) -> i=6, check[6]=true; f(6,7);

f(6,7)-> check={1,1,1,1,0,1,1},  true이면 num[i]출력 =>1,2,3,4,6,7 \n  f(6,7)종료, f(5,6)로 돌아감.

.

.

.

이런식으로 재귀와 반복분이 진행되는 코드이다. 

import java.util.*;

public class Main {
	
	static int K,S,cnt=0;
	static int[] num;
	static boolean[] check;
	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;
			num= new int[K];	//입력 담는 배열 
			check= new boolean[K];
			
			for(int i=0;i<K;i++)
				num[i]=sc.nextInt();
			
			f(0,0);
			System.out.println();
		}
		
	}// main()
	
	static void f(int dp,int start) {
		if(dp==6) {
			for(int i=0;i<K;i++){
				if(check[i]){
                    System.out.print(num[i]+" ");
                }
			}
			System.out.println();
		}
		
		for(int i=start;i<K;i++) {
			check[i]=true;
			f(dp+1,i+1);
			check[i]=false;
		}
		
	}//f()
	
}// class Main
728x90
반응형

댓글