[JS] 10818번 - 최소,최대

박세현·2021년 5월 21일
0

알고리즘

목록 보기
2/19

최소,최대

문제 출처

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


key point

// 입력
5	// 배열의 원소 개수
20 10 35 30 7 // 주어진 수

// split('\n') 입력을 개행문자로 split
// input = ['5', '20 10 35 30 7']
let input = require('fs').readFileSync('/dev/stdin').toString().split('\n');

// 주어진 수를 공백으로 split 후 원소들을 Number type으로 변환
// numArr = [20, 10, 35, 30, 7]
const numArr = input[1].split(' ').map((x) => Number(x));

풀이

문제에서는 배열의 원소 개수를 입력값으로 줬지만 어차피 원소 배열을 전부 순환해야하므로 for문이 아닌 map을 사용해서 배열의 원소 개수는 사용하지 않았다.

let input = require('fs').readFileSync('/dev/stdin').toString().split('\n');

const numArr = input[1].split(' ').map((x) => Number(x));

// 주어진 수들의 첫번째 값을 최소,최대의 기본값으로 세팅
let min = numArr[0];
let max = numArr[0];

numArr.map((num) => {
  if (num < min) {
    min = num;
  }

  if (num > max) {
    max = num;
  }
});

console.log(`${min} ${max}`);

profile
Front End 공부노트

0개의 댓글

관련 채용 정보