[백준][15665번] N과 M (11)
Updated:
문제 URL
https://www.acmicpc.net/problem/15665
나의 풀이
package backjoon;
import java.io.*;
import java.util.*;
public class Main {
static int[] ans = new int[10];
static int[] num = new int[10];
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
StringTokenizer str = new StringTokenizer(br.readLine());
int n = Integer.parseInt(str.nextToken());
int m = Integer.parseInt(str.nextToken());
StringTokenizer str2 = new StringTokenizer(br.readLine());
int[] temp = new int[n];
for (int i = 0; i < n; i++) {
temp[i] = Integer.parseInt(str2.nextToken());
}
Arrays.sort(temp);
int j = 1;
num[0] = temp[0];
for (int i = 1; i < n; i++) {
if (num[j - 1] != temp[i]) {
num[j] = temp[i];
j++;
}
}
go(0, j, m);
bw.flush();
bw.close();
}
public static void go(int idx, int n, int m) throws IOException {
if (idx == m) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < m; i++) {
sb.append(num[ans[i]] + " ");
}
bw.write(sb.toString());
bw.newLine();
return;
}
for (int i = 0; i < n; i++) {
ans[idx] = i;
go(idx + 1, n, m);
}
}
}
첫번째 다른풀이
import java.util.*;
public class Main {
static boolean[] c = new boolean[10];
static int[] a = new int[10];
static int[] num = new int[10];
static StringBuilder go(int index, int n, int m) {
if (index == m) {
StringBuilder sb = new StringBuilder();
for (int i=0; i<m; i++) {
sb.append(num[a[i]]);
if (i != m-1) sb.append(" ");
}
sb.append("\n");
return sb;
}
StringBuilder ans = new StringBuilder();
for (int i=0; i<n; i++) {
//if (c[i]) continue;
c[i] = true;
a[index] = i;
ans.append(go(index+1, n, m));
c[i] = false;
}
return ans;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[] temp = new int[n];
for (int i=0; i<n; i++) {
temp[i] = sc.nextInt();
}
Arrays.sort(temp);
int k = 0;
for (int i=0; i<n; i++) {
if (i > 0 && temp[i] == temp[i-1]) continue;
num[k++] = temp[i];
}
n = k;
System.out.print(go(0,n,m));
}
}
두번째 다른풀이
import java.util.*;
class Result implements Comparable<Result> {
Integer[] a;
Result(ArrayList<Integer> a) {
this.a = a.toArray(new Integer[a.size()]);
}
int get(int index) {
return (int)this.a[index];
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Result) {
Result that = (Result)obj;
int n = this.a.length;
for (int i=0; i<n; i++) {
if (this.a[i] != that.a[i]) {
return false;
}
}
return true;
} else {
return false;
}
}
public int compareTo(Result that) {
int n = this.a.length;
for (int i=0; i<n; i++) {
if (this.a[i] == that.a[i]) continue;
if (this.a[i] < that.a[i]) return -1;
if (this.a[i] > that.a[i]) return 1;
}
return 0;
}
}
public class Main {
static boolean[] c = new boolean[10];
static int[] a = new int[10];
static int[] num = new int[10];
static ArrayList<Result> d = new ArrayList<>();
static void go(int index, int start, int n, int m) {
if (index == m) {
ArrayList<Integer> temp = new ArrayList<>();
for (int i=0; i<m; i++) {
temp.add(num[a[i]]);
}
d.add(new Result(temp));
return;
}
for (int i=start; i<n; i++) {
//if (c[i]) continue;
c[i] = true;
a[index] = i;
go(index+1, 0, n, m);
c[i] = false;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
for (int i=0; i<n; i++) {
num[i] = sc.nextInt();
}
Arrays.sort(num, 0, n);
go(0, 0, n, m);
Collections.sort(d);
StringBuilder sb = new StringBuilder();
for (int i=0; i<d.size(); i++) {
if (i == 0 || !d.get(i).equals(d.get(i-1))) {
for (int j=0; j<m; j++) {
sb.append(d.get(i).get(j));
if (j != m-1) sb.append(' ');
}
sb.append('\n');
}
}
System.out.print(sb);
}
}
Leave a comment