티스토리 뷰

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

const fs = require('fs');
const input = fs.readFileSync("/dev/stdin").toString().trim().split(" ").map(v=>+v);

const N = input[0];
const M = input[1];
const arr = [];


for(let i = 1; i<=N; i++){
  arr.push(String(i));
}


function countBits(value){ 
  let count  = 0; 
  while(value>0){ 
    if((value&1)==1) count++; 
    value = value>>1; 
  }
  return count; 
}

function solve(){ 
  let answer = [];
  for( let i  = 0; i<(1<<N); i++){ 
    if(countBits(i)==M){ 
      let str =''; 
      for( let j=0; j<N; j++){ 
        if(i&(1<<j)) str+=arr[j]+' '; 
      }
      answer.push(str);
    }
  }

  answer.sort();
  for(let n = 0; n<answer.length; n++){
    console.log(answer[n]); 
  }
}

solve();
728x90
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
글 보관함