끄적끄적 코딩
article thumbnail
Published 2023. 1. 24. 01:32
[Java] 백준 6603번 로또 알고리즘

중복되지 않는 조합 문제입니다

DFS를 사용해서 모든 경우의 수를 찾아서 출력해주었습니다.

import java.io.*;
import java.util.*;

public class Main {
  public static BufferedReader br;
  public static BufferedWriter bw;
  public static int n;
  public static int size = 6;
  public static int[] arr;
  public static int[] box;
  public static void main(String argc[]) throws IOException{
    br = new BufferedReader(new InputStreamReader(System.in));
    bw = new BufferedWriter(new OutputStreamWriter(System.out));
    
    while(true) {
      StringTokenizer st = new StringTokenizer(br.readLine());
      n = Integer.parseInt(st.nextToken());
      if(n == 0) {
    	  break;
      }
      arr = new int[n];
      box = new int[6];
      for(int i=0; i<n; ++i){
        arr[i] = Integer.parseInt(st.nextToken());
      }
      for(int i=0; i<n; ++i){
        DFS(i, 0);
      }
      bw.write("\n");
    }
    bw.close();
  }
  public static void DFS(int index, int cnt) throws IOException{
    box[cnt] = arr[index];
    if(cnt+1 == size){
      for(int i=0; i<size; ++i){
        bw.write(box[i] + " ");
      }
      bw.write("\n");
      return;
    }
    for(int i = index+1; i<n; ++i){
      DFS(i, cnt+1);
    }
  }
}

'알고리즘' 카테고리의 다른 글

[Java] 백준 2784번 가로 세로 퍼즐  (0) 2023.01.24
[Java] 백준 13458번 시험 감독  (0) 2023.01.24
[Java] 백준 15650번 N과 M (2)  (0) 2023.01.23
[Java] 백준 15649번 N과 M (1)  (0) 2023.01.23
[Java] 백준 1260번 DFS와 BFS  (0) 2023.01.23

검색 태그