티스토리 뷰

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

 

6603번: 로또

입력은 여러 개의 테스트 케이스로 이루어져 있다. 각 테스트 케이스는 한 줄로 이루어져 있다. 첫 번째 수는 k (6 < k < 13)이고, 다음 k개 수는 집합 S에 포함되는 수이다. S의 원소는 오름차순으로

www.acmicpc.net

const fs = require('fs');
const input = fs.readFileSync("./dev/stdin").toString().trim().split("\n").map(v=>v.split(' ').map(x=>+x));
input.pop();
const answer = []; 

input.forEach(v=>{
  let result = [];
  if(answer.length>0) answer.push([])
  const N = +v.shift();
  for(let i =0; i<1<<N; i++){
     if(countBits(i)==6){
       let temp = [];
       let value = i;
      for(let j = 0; j<N; j++){
        if((value&1)==1) temp.push(v[j]);
        value = value>>1;
      }
      result.push(temp)
     }
  }
  answer.push(sortSix(result))
})


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

console.log(answer.join('\n'))

function sortSix(arr){
  return arr.sort((a,b)=>{
    if(a[0]>b[0]){
      return 1
    }else if(a[0]<b[0]){
      return -1
    }else{
      if(a[1]>b[1]){
        return 1
      }else if(a[1]<b[1]){
        return -1
      }else{
        if(a[2]>b[2]){
          return 1
        }else if(a[2]<b[2]){
          return -1
        }else{
          if(a[3]>b[3]){
            return 1
          }else if(a[3]<b[3]){
            return -1
          }else{
            if(a[4]>b[4]){
              return 1
            }else if(a[4]<b[4]){
              return -1
            }else{
              if(a[5]>b[5]){
                return 1
              }else if(a[5]<b[5]){
                return -1
              }
            }
          }
        }
      }
    }
  }).map(v=>v.join(' ')).join('\n')
}
728x90
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/02   »
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
글 보관함