치춘짱베리굿나이스

[백준] 18301 본문

Javascript + Typescript/자바스크립트로 알고리즘풀기

[백준] 18301

치춘 2022. 7. 14. 01:37

Rats

문제

To celebrate the Lunar New Year of the Rat, Douglas decides to count the number of rats living in his area. It is impossible for him to find all rats, as they tend to be well hidden. However, on the first day of the new year, Douglas manages to capture n1 rats, and marks each of them with an ear tag before releasing them. On the second day of the new year, Douglas captures n2 rats, and observes that n12 of them had been marked during the first day.

Douglas is asking for your help to estimate the total number of rats in his area. Looking up in your statistics textbook, you propose using the Chapman estimator N, given by:

N := ⌊(n1 + 1)(n2 + 1)/(n12 + 1) - 1⌋

where ⌊x⌋ is the floor of a real number x, i.e., the closest integer less than or equal to x.

입력

The input consists of a single line, with three space-separated integers: n1, n2, n12, in that order.

출력

The output should contain a single line with the single integer N.

풀이

const rats = () => {
  let n = require("fs")
    .readFileSync("/dev/stdin")
    .toString()
    .trim()
    .split(" ")
    .map(Number);
  console.log(Math.floor(Math.abs(((n[0] + 1) * (n[1] + 1)) / (n[2] + 1) - 1)));
};

rats();

반성회

⌊x⌋ 기호가 x의 절대값과 제일 가까운 (작거나 같은) 정수이므로, Math.floorMath.abs를 이용한다

'Javascript + Typescript > 자바스크립트로 알고리즘풀기' 카테고리의 다른 글

[백준] 25311  (0) 2022.07.14
[백준] 25314  (0) 2022.07.14
[백준] 18096  (0) 2022.07.14
[백준] 17256  (0) 2022.07.13
[백준] 3733  (0) 2022.07.13
Comments