본문 바로가기
개발 노트/백준, 프로그래머스 풀이

[백준 14916/javascript] 거스름돈

by tokkiC 2022. 10. 30.

동전 그리디 기초 문제

기초 of 기초라...뭐라 할 말이...

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

 

14916번: 거스름돈

첫째 줄에 거스름돈 액수 n(1 ≤ n ≤ 100,000)이 주어진다.

www.acmicpc.net

let input = [];

const readline = require("readline").createInterface({
  input: process.stdin,
  output: process.stdout,
});

readline.on("line", (line) => {
  input.push(line);
});

readline.on("close", () => {
  solution(input);
  process.exit();
});

const solution = (inp) => {
  let n = Number(inp[0]);
  let cnt = 0;
  let w5 = Math.floor(n / 5);
  let i = w5;

  while (true) {
    let tn = n;

    // 5원짜리가 없는 경우까지 모두 센다면
    if (i < 0) {
      cnt = -1;
      break;
    }

    if ((tn - 5 * i) % 2 === 0) {
      let rest = tn - 5 * i;
      cnt += i + rest / 2;
      break;
    }

    i--;
  }

  console.log(cnt);
};

댓글