🎯 사전 스터디 배열 문제
1. 수열 최솟값 위치
수열이 주어질 때 ,이 수열의 있는 수 중 최소값의 위치를 모두 출력하는 프로그램 작성. 입력은 자연수로 된 배열을 받고, 시작 위치는 0으로 계산하여 최소값의 위치를 배열로 반환한다. 모든 수는 100 이하의 자연수로 입력 받는다.
입력)
[5, 2, 10, 2],
[4, 5, 7, 4, 8],
[12, 11, 11, 16, 11, 12],
결과)
[1, 4],
[0, 3],
[1, 2, 4]
let array = [5, 2, 10, 2]
array.min()
const arr = [5, 2, 10, 2];
const min = Math.min(...arr);
const index = arr.indexOf(min);
console.log(index);
let arr = [5, 2, 10, 2];
let min = Math.min.apply(Math, arr); // min 값 찾기
let count = 0; // count 변수 지정
let indexes = []; //min num를 저장하는 new array 생성
for (let i = 0; i < arr.length; i++) { // for looping the array
if (arr[i] == min) { // if the value is equal to the min number
indexes.push(i); // push the index to the index array
count++ // increment the counter
}
} console.log(indexes)
// [1, 3]
let arr = [4, 5, 7, 4, 8];
let min = Math.min.apply(Math, arr);
let count = 0;
let indexes = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i] == min) {
indexes.push(i);
count++
}
} console.log(indexes)
// [0, 3]
let arr = [12, 11, 11, 16, 11, 12];
let min = Math.min.apply(Math, arr);
let count = 0;
let indexes = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i] == min) {
indexes.push(i);
count++
}
} console.log(indexes)
// [1, 2, 4]
2. 체스 세트
체스판과 체스 기물이 완전히 한 세트를 이루지 않고 있다.
게임을 위해 부족하거나 많은 기물의 개수를 계산하여 반환하는 프로그램을 작성하라.
기물의 개수는 배열 형태로 king부터 pawns 순으로 들어오며
한 게임을 위해 필요한 기물의 개수는 다음과 같다.
입력)
[0, 1, 2, 2, 2, 7],
[2, 1, 2, 1, 2, 1],
[0, 1, 1, 5, 3, 6],
결과)
[ 1, 0, 0, 0, 0, 1 ][ -1, 0, 0, 1, 0, 7 ]
[ 1, 0, 1, -3, -1, 2 ]
3. 두 수 최대 합
수열이 주어질 때, 두 개의 수를 선택하여 최대 합이 나올 수 있는 프로그램을 제작하시오.
입력은 정수로 된 배열을 받고, 최대 합이 나올 수 있는 두 수를 배열 형태로 반환한다.
배열로 입력되는 정수는 10 ~ 20개 사이이며, 범위는 -20 ~ +20 사이의 값이 입력된다.
입력)
[-11, 5, 18, -2, -3, 6, 4, 17, 10, 9],
[3, 7, -14, 2, -6, 13, -20, -2, -7, 6, -17, -5, 14, -9, 19],
[-15, -4, -8, 12, 12, -8, -8, 9, 10, 15, -2, 10, -14, 2, 13, 19, -9, 3, -18, 14]
결과)
[ 18, 17 ][ 19, 14 ]
[ 19, 15 ]
function twoHighest(arr) {
let highest = 0;
let secondHighest = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] >= highest) { // >= in here, an explanation is above
secondHighest = highest; // firstly, move the ex-highest to the second place
highest = arr[i];
} else if (arr[i] > secondHighest && arr[i] < highest) {
secondHighest = arr[i];
}
}
return [secondHighest, highest];
} console.log(twoHighest([-11, 5, 18, -2, -3, 6, 4, 17, 10, 9]))
// [17, 18]
function twoHighest(arr) {
let highest = 0;
let secondHighest = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] >= highest) { // >= in here, an explanation is above
secondHighest = highest; // firstly, move the ex-highest to the second place
highest = arr[i];
} else if (arr[i] > secondHighest && arr[i] < highest) {
secondHighest = arr[i];
}
}
return [secondHighest, highest];
} console.log(twoHighest([3, 7, -14, 2, -6, 13, -20, -2, -7, 6, -17, -5, 14, -9, 19]))
// [14, 19]
function twoHighest(arr) {
let highest = 0;
let secondHighest = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] >= highest) { // >= in here, an explanation is above
secondHighest = highest; // firstly, move the ex-highest to the second place
highest = arr[i];
} else if (arr[i] > secondHighest && arr[i] < highest) {
secondHighest = arr[i];
}
}
return [secondHighest, highest];
} console.log(twoHighest([-15, -4, -8, 12, 12, -8, -8, 9, 10, 15, -2, 10, -14, 2, 13, 19, -9, 3, -18, 14]))
// [15, 19]
https://stackoverflow.com/questions/61069247/return-the-two-highest-numbers-in-an-array-javascript