반응형
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 |
Tags
- 양평
- BigData
- 코딩
- Data Engineer
- 코테
- 알고리즘
- apache iceberg
- 삼성역맛집
- 코엑스맛집
- 코엑스
- 영어
- Data Engineering
- 파이썬
- 맛집
- 코딩테스트
- BFS
- 자바
- 백준
- bigdata engineering
- dfs
- 여행
- java
- 프로그래머스
- Iceberg
- HIVE
- bigdata engineer
- hadoop
- 개발
- Trino
- 용인맛집
Archives
- Today
- Total
지구정복
[브루트포스, 백트래킹] 백준 - N과 M (2) 본문
728x90
반응형
https://www.acmicpc.net/problem/15650
15650번: N과 M (2)
한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해
www.acmicpc.net
-자바
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int n, m;
static int[] arr;
static StringBuilder sb = new StringBuilder();
public static void dfs( int index, int depth ) {
if( depth == m ) {
for( int val : arr ) sb.append( val ).append( " " );
sb.append( "\n" );
return;
}
for( int i=index; i<=n; i++ ) {
arr[depth] = i;
dfs( i+1, depth+1 );
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer( br.readLine() );
n = Integer.parseInt( st.nextToken() );
m = Integer.parseInt( st.nextToken() );
arr = new int[m];
dfs( 1, 0 );
System.out.println( sb );
}
}
-파이썬
from sys import stdin
def dfs( index, depth ):
global n, m, arr, s
if depth == m:
for val in arr: s += str(val)+" "
s += "\n"
return
for i in range( index, n+1 ):
arr[depth] = i
dfs( i+1, depth+1 )
input = stdin.readline
n, m = map(int, input().split())
arr = [0]*m
s = ""
dfs( 1, 0 )
print( s )
728x90
반응형
'데이터 엔지니어링 정복 > Algorithm' 카테고리의 다른 글
[완전탐색&백트래킹] 백준 - NM과 K (0) | 2021.09.01 |
---|---|
[브루트포스&백트래킹] 백준 - N과 M (8) (0) | 2021.09.01 |
[브루트포스, 백트래킹] 백준 - N과 M (7) (0) | 2021.08.31 |
[브루트포스, 백트래킹] 백준 - N과 M (6) (0) | 2021.08.31 |
[브루트포스, 백트리킹] 백준 - N과 M (5) (0) | 2021.08.31 |