Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 배열
- 코딩
- 다이나믹프로그래밍
- 동적계획법
- 프로그래머스
- activity
- Android
- BFS
- 알고리즘
- 생명주기
- 문자열다루기
- 분할정복
- BOJ
- 운영체제
- 이분탐색
- component
- 백준
- GIT
- 카카오블라인드
- 세그먼트트리
- 문자열
- 자바
- 스택
- 완전탐색
- 트리
- 코딩테스트
- 안드로이드
- 그래프
- 코틀린
- dfs
Archives
- Today
- Total
HS_development_log
백준 6603 - 로또 / Java 본문
반응형
1. 문제
2. 알고리즘
-
k개의 수 중에서 6개를 조합하는 문제이다. 경우의 수는 kC6.
-
재귀로 완전탐색을 실시하여 모든 조합을 찾은 후 출력한다.
-
재귀의 return조건은 6개를 조합해야하므로 detph==6.
3. 코드
/**
* 2020.07.28
* 백준 6603 : 로또
* DevHyeonseong
*/
import java.util.*;
public class BOJ6603 {
public static int[] lotto;
public static int[] answer;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String input = scan.nextLine();
while(!input.equals("0")){
String[] temp = input.split(" ");
lotto = new int[temp.length-1];
answer = new int[temp.length-1];
int k = Integer.parseInt(temp[0]);
for(int i=1;i<temp.length;i++){
lotto[i-1] = Integer.parseInt(temp[i]);
}
dfs(0,0, k);
System.out.println();
input = scan.nextLine();
}
}
public static void dfs(int idx, int depth,int k){
if(depth==6){ // 6개의 순열이 만들어지면 출력후 return
StringBuilder stringBuilder = new StringBuilder();
for(int i=0;i<answer.length;i++){ // 자바의 System 호출을 여러번 하는것은 효율이 좋지 않으므로 한꺼번에 출력한다.
if(answer[i]==0){
break;
}
stringBuilder.append(answer[i]+" ");
}
System.out.println(stringBuilder.toString().trim());
return;
}
for(int i=idx;i<k;i++){
answer[depth] = lotto[i];
dfs(i+1,depth+1,k);
}
}
}
끝.
반응형
'Algorithm-백준 > BFS,DFS' 카테고리의 다른 글
백준 10026번 - 적록색약 / Java (0) | 2020.08.27 |
---|---|
백준 4963번 - 섬의 개수 / Kotlin (0) | 2020.08.04 |
백준 7562번 - 나이트의 이동 / Java (0) | 2020.02.05 |
백준 7576번 - 토마토 / Java (0) | 2020.02.05 |
백준 13023번 - ABCDE / Java (0) | 2020.02.05 |