본문 바로가기
코딩테스트(알고리즘)/baekjoon

[baekjoon] 5635 생일 (Javascript)

by Cafe Mocha 2022. 6. 24.

5635번: 생일 (acmicpc.net)

 

5635번: 생일

어떤 반에 있는 학생들의 생일이 주어졌을 때, 가장 나이가 적은 사람과 가장 많은 사람을 구하는 프로그램을 작성하시오.

www.acmicpc.net


접근 : 구현, map

 

Map()을 활용해 볼 수 있는 좋은 문제

 

Javascript

let input = require("fs")
  .readFileSync("input.txt") //"/dev/stdin"
  .toString()
  .split("\n")
  .map((val) => val.trim());
function solution(input) {
  //input
  let n = +input.shift();
  let arr = [];

  for (let i = 0; i < n; i++) {
    let temp = input.shift().split(" ");
    arr.push(temp);
  }

  let birth = new Map();
  let num = [];
  arr.forEach((v) => {
    let year = v[3];
    let month = v[2];
    let day = v[1];

    if (month.length === 1) {
      month = "0" + month;
    }
    if (day.length === 1) {
      day = "0" + day;
    }
    let temp = [year, month, day].join("");
    num.push(temp);
    birth.set(temp, v[0]);
  });

  let old = String(Math.min(...num));
  let young = String(Math.max(...num));

  console.log(birth.get(young));
  console.log(birth.get(old));
}

solution(input);