선택 언어 : nodejs
백준 배열 문제 모음 : 🔗 BaaaaaaaaaaarkingDog 0x03강 - 배열
let [N, K] = require('fs').readFileSync('./input.txt').toString().trim().split(' ').map(Number)
let que = [...Array(N)].map((_, idx) => idx + 1) // [1, 2, 3, 4, 5, 6, 7]
let answer = [],
count = 1
while (que.length > 0) {
const shiftItem = que.shift()
if (count % K === 0) {
answer.push(shiftItem)
} else {
que.push(shiftItem)
}
count++
}
console.log(`<${answer.join(', ')}>`)
const [N, K] = require('fs').readFileSync('./input.txt').toString().trim().split(' ').map(Number)
let arr = [...Array(N)].map((_, idx) => idx + 1) // [1, 2, 3, 4, 5, 6, 7]
let pick = 0
let answer = []
while (arr.length) {
pick = (pick + K - 1) % arr.length // ① 인덱스 번호니까 K -1 번째 ②꺼낼 번째 수가 arr.length를 넘어가지 않도록
answer.push(arr.splice(pick, 1))
}
console.log('<' + answer.join(', ') + '>')
const input = require('fs').readFileSync('./input.txt').toString().trim()
let count = Array(10).fill(0) // 인덱스 번호 === 카드 번호
for (let i = 0; i < input.length; i++) {
if (input[i] === '9' || input[i] === '6') {
count[9] >= count[6] ? count[6]++ : count[9]++
} else {
count[+input[i]]++
}
}
console.log(Math.max(...count))
// '/dev/stdin'
let [counts, input, num] = require('fs').readFileSync('./input.txt').toString().trim().split('\n')
counts = Number(counts)
input = input.split(' ').map(Number)
num = Number(num)
// counts: 11
// input: [ 1, 4, 1, 2, 4, 2, 4, 2, 3, 4, 4 ]
// num 2
function solution(input, num) {
const answer = input.filter(ele => ele === num)
console.log(answer.length)
}
solution(input, num)
const input = require('fs').readFileSync('./input.txt').toString().trim().split('')
const alphabet = {}
for (let i = 97; i < 123; i++) {
alphabet[String.fromCharCode(i)] = 0
}
input.forEach(el => alphabet[el]++)
console.log(...Object.values(alphabet))
const [[N, capacity], ...input] = require('fs')
.readFileSync('./input.txt')
.toString()
.trim()
.split('\n')
.map(el => el.split(' ').map(Number))
const students = {}
for (let i = 1; i <= 6; i++) {
students[i] = [0, 0]
}
for ([gender, grade] of input) {
gender ? students[grade][1]++ : students[grade][0]++
}
/* students: {
'1': [ 1, 2 ],
'2': [ 2, 1 ],
'3': [ 1, 3 ],
'4': [ 0, 1 ],
'5': [ 1, 2 ],
'6': [ 1, 1 ]
} */
let flatAry = Object.values(students).flat() // [ 1, 2, 2, 1, 1, 3, 0, 1, 1, 2, 1, 1 ]
let answer = flatAry.reduce((acc, cur) => {
return (acc += cur ? Math.ceil(cur / capacity) : 0)
}, 0)
console.log(answer)