[BOJ 6603] 로또 (java)
문제 링크 https://www.acmicpc.net/problem/6603
문제풀이
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package basics_600; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
public class BOJ6603 { | |
static int[] arr; | |
static boolean[] visited; | |
public static void main(String[] args) throws IOException { | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
while (true) { | |
String[] st = br.readLine().split(" "); | |
int N = Integer.parseInt(st[0]); | |
arr = new int[N]; | |
visited = new boolean[N]; | |
if (N == 0) | |
break; | |
// 집합 S입력 | |
for (int i = 1; i <= N; i++) { | |
arr[i-1] = Integer.parseInt(st[i]); | |
} | |
//집합 S에서 6개 뽑는 조합의 수와 같다. | |
//0번째 인덱스 있는 애부터 시작~6번째 인덱스까지 | |
makeLotto(arr, visited, 0, 6); | |
System.out.println(); | |
} | |
} | |
static void makeLotto(int[] arr, boolean[] visited, int start, int r) { | |
if(r==0) { //더이상 뽑을게 남아있지 않다면 뽑힌 숫자들 출력 | |
print(arr,visited); | |
return; | |
}else { | |
for(int i=start;i<arr.length;i++) { | |
visited[i] = true; //로또 번호로 뽑혔다면 true | |
makeLotto(arr,visited,i+1,r-1); //하나 뽑았으니까 다음거부터 시작해서 크기-1만큼 다시 반복 | |
visited[i] = false; | |
} | |
} | |
} | |
static void print(int[] arr, boolean[] visited) { | |
for(int i=0;i<arr.length;i++) { | |
if(visited[i]) | |
System.out.print(arr[i]+" "); | |
} | |
System.out.println(); | |
} | |
} |
Comments