https://www.acmicpc.net/problem/2562
// map의 첫번째 인자는 요소
// 두번째 인자는 배열의 index
// numArr = [1, 2, 3, 4, 5]
numArr.map((num, idx) => {
console.log(`${num} ${idx}`);
}
/*
결과값
1, 0
2, 1
3, 2
4, 3
5, 4
*/
입력값을 배열로 저장한 후 map을 이용해서 최대값을 구했다.
최대값이 몇 번째 수인지 구하는 것은 배열의 index에 1을 더하는 것으로 (배열의 index는 0부터 시작하므로) 구할 수 있다.
let input = require('fs').readFileSync('/dev/stdin').toString().split('\n');
const numArr = input.map((x) => Number(x));
let max = numArr[0];
let index = 1;
numArr.map((num, idx) => {
if (num > max) {
max = num;
index = idx + 1;
}
});
console.log(`${max}\n${index}`);