[BOJ]15654_N과 M(5).java
1.문제
2.코드
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
/**
* Author : YoungSeo Jeon
* Date : 2021-09-13
* Description : 백준 15654
*/
public class Main{
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static int M, N ;
static int[] arr;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
M = Integer.parseInt(st.nextToken());
N = Integer.parseInt(st.nextToken());
arr = new int[M];
visited = new boolean[M];
st = new StringTokenizer(br.readLine());
for(int i=0; i<M; i++)
arr[i] = Integer.parseInt(st.nextToken());
Arrays.sort(arr);
StringBuilder sb = new StringBuilder();
permutation(sb,0);
bw.flush();
br.close();
bw.close();
}
static boolean[] visited;
public static void permutation(StringBuilder sb,int n) throws IOException {
if(n == N) {
bw.write(sb.toString()+"\n");
return ;
}
for(int i=0; i<M; i++) {
if(visited[i]) continue;
sb.append(arr[i]+" ");
visited[i] = true;
permutation(sb,n+1);
sb.setLength(sb.length()-1);
if(sb.lastIndexOf(" ")!=-1) {
sb.setLength(sb.lastIndexOf(" "));
sb.append(" ");
}else {
sb.setLength(0);
}
visited[i] = false;
}
}
}
3.Review
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
/**
* Author : YoungSeo Jeon
* Date : 2021-09-13
* Description : 백준 15654
*/
public class Main{
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static int M, N ;
static int[] arr;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
M = Integer.parseInt(st.nextToken());
N = Integer.parseInt(st.nextToken());
arr = new int[M];
visited = new boolean[M];
st = new StringTokenizer(br.readLine());
for(int i=0; i<M; i++)
arr[i] = Integer.parseInt(st.nextToken());
Arrays.sort(arr);
StringBuilder sb = new StringBuilder();
permutation(sb,0);
bw.flush();
br.close();
bw.close();
}
static boolean[] visited;
public static void permutation(StringBuilder sb,int n) throws IOException {
if(n == N) {
bw.write(sb.toString()+"\n");
return ;
}
for(int i=0; i<M; i++) {
if(visited[i]) continue;
sb.append(arr[i]+" ");
visited[i] = true;
permutation(sb,n+1);
sb.setLength(sb.length()-1);
if(sb.lastIndexOf(" ")!=-1) {
sb.setLength(sb.lastIndexOf(" "));
sb.append(" ");
}else {
sb.setLength(0);
}
visited[i] = false;
}
}
}
StringBuilder를 이용하여 적절히 바꿔주었다.
Author And Source
이 문제에 관하여([BOJ]15654_N과 M(5).java), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@hakka_ame/BOJ15654N과-M5.java저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)