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

[백준 14490/javascript] 백대열

by tokkiC 2022. 9. 13.

유클리드 호제법을 이용해서 두 수의 최대 공약수를 구한뒤

각 수를 나눠주면 되는 문제이다

유클리드 호제법의 사용법을 묻는 문제였다

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

 

14490번: 백대열

n과 m이 :을 사이에 두고 주어진다. (1 ≤ n, m ≤ 100,000,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, m] = inp[0].split(":").map((el) => Number(el));
  let gcd = (a, b) => {
    while (b > 0) {
      let r = a % b;
      a = b;
      b = r;
    }
    return a;
  };
  let num = gcd(n, m);
  console.log(`${Number(n / num)}:${Number(m / num)}`);
};

댓글