본문 바로가기

BOJ/수학

BOJ : 9375 패션왕신해빈 (JS)

728x90

소스코드

let fs = require('fs');
const filePath = process.platform === "linux" ? `/dev/stdin` : `예제.txt`; 
let input = fs.readFileSync(filePath).toString().trim().split('\n');
const c = console.log

n = Number(input[0])

let array = [];

//입력값 받기
for (let i = 1; i < input.length; i++) {
    if (input[i] !== '') {
        array.push(input[i].split(' '));
    }
}

for (let i = 0 ; i<array.length ; i++){
    if (array[i].length == 2 ) {
        array[i][1] = array[i][1].replace(/\r/,"")
    } 
    else if (array[i].length == 1) {
        array[i][0] = array[i][0].replace(/\r/,"")
    }
}

let lst = [] 
let dict = {}
let resultOutput = []
let count = 0

for (let i=0 ; i < array.length ; i++ ){
    
    let result = 1
    

    if (array[i].length == 2 ) {
        lst.push(array[i][1])
    } 
    else if (array[i].length == 1) {
        count = Number(array[i][0])
    }
    
    if (count == lst.length) {

        for (let v of lst) {
            dict[v] = dict[v] ? dict[v] + 1 : 1
        }

        for (let k in dict) {
            result = result * (dict[k]+1)
        }
        lst = []
        dict={}
        resultOutput.push(result-1)
    }

}

for (let i = 0 ; i < n ; i++){
    c(resultOutput[i])
}

 

풀이

의상은 종류별로 1개씩 입을 수 있으니 입력1과 같은 상황에서 headgear를 착용안할때와 착용할때를 계산하기 위해서 갯수 +1 를 해준 후 곱셈을 해주었다.

그런데 옷을 하나도 안 입는 경우도 있기때문에 마지막에 -1를 해주었다.

 

문제

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

 

9375번: 패션왕 신해빈

첫 번째 테스트 케이스는 headgear에 해당하는 의상이 hat, turban이며 eyewear에 해당하는 의상이 sunglasses이므로   (hat), (turban), (sunglasses), (hat,sunglasses), (turban,sunglasses)로 총 5가지 이다.

www.acmicpc.net

 

728x90

'BOJ > 수학' 카테고리의 다른 글

BOJ : 2004 조합0의 개수 (JS)  (0) 2022.12.22
BOJ : 1676 팩토리얼 0의 개수 (JS)  (0) 2022.12.22
BOJ : 3036 링 (파이썬)  (1) 2022.12.20
★ BOJ : 2981 검문 (파이썬) ★  (0) 2022.12.20
BOJ : 1934 최소공배수 (파이썬)  (0) 2022.12.20