BOJ 10974번: 다음 순열 문제
function nextPermutation(arr) {
let i = arr.length - 2,
j = arr.length - 1;
while (arr[i + 1] <= arr[i]) {
i--;
}
if (i < 0) {
return false;
}
while (arr[j] <= arr[i]) {
j--;
}
[arr[i], arr[j]] = [arr[j], arr[i]];
const next = arr.slice(i + 1).reverse();
arr.splice(i + 1, next.length, ...next);
return arr;
}
function main () {
const fs = require("fs");
const n = Number(fs.readFileSync("/dev/stdin").toString());
const arr = Array.from({ length: n }, (_, i) => i + 1);
do {
console.log(arr.join(" "));
} while (nextPermutation(arr));
}
main();
정말 응용할 곳이 많겠는걸요