티스토리 뷰

728x90

문제 링크

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

 

25285번: 심준의 병역판정검사

병역판정검사는 병역의무자들의 상태를 검사해 징병 여부와 징병 시 어느 방향으로 복무를 시키는 게 좋을지 판정하는 검사로, 남성들은 만 19세가 되는 해에 모두 병역판정검사를 받는다. 신체

www.acmicpc.net

 

풀이

여러 개의 조건문을 이용하여 풀어주면 됩니다.

저 같은 경우는 명확히 키만으로 급수가 나오는 경우를 classifyWithHeight() 함수로, 그 외의 경우를 classifyWithBMI() 함수로 빼서 판단했습니다.

 

 

정답 코드

// @BOJ ------------------------------------------
const fs = require('fs');
const stdin = fs.readFileSync('/dev/stdin').toString().split('\n');
const input = (() => {
  let line = 0;
  const input = () => stdin[line++];
  input.num = () => input().split(' ').map(Number);
  input.rows = l => Array(l).fill().map(input);
  input.rows.num = l => Array(l).fill().map(input.num);
  return input;
})();

// Solution -----------------------------------

const square = num => num * num;
const isInRange = value => (l, r) => l <= value && value < r;
const calcBMI = (height, weight) => weight / square(height / 100);
const classifyWithHeight = height => {
  if (height < 140.1) return 6;
  else if (height < 146) return 5;
  else if (height < 159 || height >= 204) return 4;
  else return -1;
};
const classifyWithBMI = (height, weight) => {
  const isBMIInRange = isInRange(calcBMI(height, weight));
  if (!isBMIInRange(16, 35)) return 4;
  else if (isInRange(height)(159, 161)) return 3;
  else if (isBMIInRange(16, 18.5) || isBMIInRange(30, 35)) return 3;
  else if (isBMIInRange(18.5, 20) || isBMIInRange(25, 30)) return 2;
  else return 1;
};
const classify = ([height, weight]) => {
  const classification = classifyWithHeight(height);
  if (classification < 0) return classifyWithBMI(height, weight);
  return classification;
};
const solution = function () {
  const t = +input();
  const ans = input.rows.num(t).map(classify).join('\n');
  console.log(ans);
};

solution();
728x90
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함