728x90
반응형
* 출처: www.acmicpc.net/problem/6603
6603번: 로또
입력은 여러 개의 테스트 케이스로 이루어져 있다. 각 테스트 케이스는 한 줄로 이루어져 있다. 첫 번째 수는 k (6 < k < 13)이고, 다음 k개 수는 집합 S에 포함되는 수이다. S의 원소는 오름차순으로
www.acmicpc.net
내 생각
기본적인 조합 문제
public class Main {
static StringBuilder sb;
static int k;
static int m = 6;
static int[] numbers;
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
String s = "";
while(!(s=in.readLine()).equals("0")) {
st = new StringTokenizer(s, " ");
k = Integer.parseInt(st.nextToken()); // k개
numbers = new int[k]; // 숫자 집합
//init number
for(int i=0;i<k;i++) {
numbers[i] = Integer.parseInt(st.nextToken());
}
// 조합
sb = new StringBuilder("");
combination(0,new int[m],0);
System.out.println(sb);
}
}
// 조합
static void combination(int toSelect, int[] selected, int startIdx) {
if(toSelect == m) {
for(int i=0;i<m;i++) {
sb.append(selected[i]).append(' ');
}
sb.append('\n');
return;
}
for(int i=startIdx;i<k;i++) {
selected[toSelect] = numbers[i];
combination(toSelect+1, selected, i+1);
}
}
}
728x90
반응형
'알고리즘 > 백준' 카테고리의 다른 글
[문제] 백준 10819번 차이를 최대로 (0) | 2021.02.05 |
---|---|
[문제] 백준 2493번 탑 (0) | 2021.02.04 |
[문제] 백준 1992번 쿼드트리 (0) | 2021.02.04 |
[문제] 백준 15664번 N과 M(10) (0) | 2021.02.04 |
[문제] 백준 2231번 분해합 (0) | 2021.02.03 |