728x90

https://www.acmicpc.net/problem/15652

😢 중복 조합 문제

😊 각 숫자를 중복으로 뽑아도 되지만,
조합된 숫자가 중복되면 안되므로 전에 뽑았던 숫자를 제외 해야한다.

// N and M (4) Combination of repetition
// For submit
// const fs = require('fs');
// const input = fs.readFileSync('/dev/stdin').toString().trim().split(' ').map(nm => parseInt(nm));
// For local test
const input = [3, 3];
const N = input.shift();
const M = input.shift();
const visited = new Array(N);
const output = [];
let result = '';
function dfs(idx, cnt) {
if (cnt === M) {
result += `${output.join(' ')}\n`;
return;
}
for (let i = idx; i < N; i++) {
output.push(i + 1);
dfs(i, output.length);
output.pop();
}
}
dfs(0, 0);
console.log(result);
view raw bj_js15652.js hosted with ❤ by GitHub

+ Recent posts