Solved.ac Class4
public class Main {
private static final StringBuilder sb = new StringBuilder();
private static int size;
private static int target;
private static int[] num;
private static int[] newNum;
private static boolean[] isVisit;
private static boolean[] newIsVisit;
private static int[] answers;
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] input = br.readLine().split(" ");
size = Integer.parseInt(input[0]);
target = Integer.parseInt(input[1]);
num = new int[size + 1];
newNum = new int[size - 1];
isVisit = new boolean[size + 1];
newIsVisit = new boolean[size - 1];
answers = new int[target];
input = br.readLine().split(" ");
for (int i = 1; i < size + 1; i++) {
num[i] = Integer.parseInt(input[i - 1]);
}
Arrays.sort(num);
for (int i = 1; i < size + 1; i++) {
answers[0] = num[i];
isVisit[i] = true;
makeNewArray();
solve(target, 1);
isVisit[i] = false;
}
System.out.println(sb);
}
private static void makeNewArray() {
int count = 0;
for (int i = 1; i < size + 1; i++) {
if (!isVisit[i]) {
newNum[count] = num[i];
count++;
}
}
}
private static void solve(int target, int deep) {
if (deep == target) {
print();
return;
}
for (int i = 0; i < size - 1; i++) {
if (!newIsVisit[i]) {
newIsVisit[i] = true;
answers[deep] = newNum[i];
solve(target, deep + 1);
newIsVisit[i] = false;
}
}
}
private static void print() {
for (int i = 0; i < target; i++) {
sb.append(answers[i]).append(" ");
}
sb.append("\n");
}
}
성공
val sb: StringBuilder = StringBuilder()
var size: Int = 0
var target: Int = 0
var newIsVisit: BooleanArray = BooleanArray(0)
var answers: IntArray = IntArray(0)
var newNum: IntArray = IntArray(0)
fun main() {
val split = readln().split(" ")
size = split[0].toInt()
target = split[1].toInt()
val num = IntArray(size + 1)
val isVisit = BooleanArray(size + 1)
newNum = IntArray(size - 1)
newIsVisit = BooleanArray(size - 1)
answers = IntArray(target)
val input = readln().split(" ")
for (i in 1..size) {
num[i] = input[i - 1].toInt()
}
num.sort()
for (i in 1..size) {
answers[0] = num[i]
isVisit[i] = true
makeNewArray(num, isVisit)
solve(target, 1)
isVisit[i] = false
}
print(sb)
}
fun makeNewArray(num: IntArray, isVisit: BooleanArray) {
var count = 0
for (i in 1..size) {
if (!isVisit[i]) {
newNum[count] = num[i]
count++
}
}
}
fun solve(target: Int, deep: Int) {
if (deep == target) {
print()
return
}
for (i in 0..<size - 1) {
if (!newIsVisit[i]) {
newIsVisit[i] = true
answers[deep] = newNum[i]
solve(target, deep + 1)
newIsVisit[i] = false
}
}
}
fun print() {
for (i in 0..<target) {
sb.append(answers[i]).append(" ")
}
sb.append("\n")
}