본문 바로가기
코테/코테withJS

백준 Node.js(자바스크립트) 2839번 설탕배달

by 트레일헤드레인저 2021. 9. 10.
SMALL

const fs = require('fs');
const filePath = process.platform === 'linux' ? '/dev/stdin' : './input.txt';
let sugar = Number(fs.readFileSync(filePath).toString()); 

solution();

function solution() {

    let smallBag = 0;
    while (true) {
            if (sugar % 5 == 0) {
                console.log(sugar / 5 + smallBag);
                break;
            } else if (sugar / 5 < 0) {
                console.log(-1);
                break;
            } else {

                sugar -= 3;
                smallBag++;
            }
        }
}

sugar % 5 == 0 이 아닐 경우 ( 5kg이 커야 봉지 개수가 적어지는데 5의 배수가 아닐경우)

sugar -= 3 

smallBag++; 을 해줌으로서 3kg가방을 하나 추가하고 다시 5의 배수체크하러감

 

while(true)에서 true말고 다른게 있는지 모르겠음

그래서 break를 잊지말고하기

LIST