문제 설명
a 배열과 b 배열에 있는 숫자들 중
a 배열의 배수이고, b 배열의 약수인 것들의 개수를 계산하는 문제
'use strict';
const fs = require('fs');
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});
process.stdin.on('end', function() {
inputString = inputString.split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
/*
* Complete the 'getTotalX' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER_ARRAY a
* 2. INTEGER_ARRAY b
*/
function getTotalX(a, b) {
// Write your code here
let answer = 0
let min = Math.max(...a)
let max = Math.min(...b)
for(let i=min; i<=max; i++) {
if(a.every(el => i % el ===0)) {
if(b.every(el => el % i === 0)) answer ++
}
}
return answer
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const firstMultipleInput = readLine().replace(/\s+$/g, '').split(' ');
const n = parseInt(firstMultipleInput[0], 10);
const m = parseInt(firstMultipleInput[1], 10);
const arr = readLine().replace(/\s+$/g, '').split(' ').map(arrTemp => parseInt(arrTemp, 10));
const brr = readLine().replace(/\s+$/g, '').split(' ').map(brrTemp => parseInt(brrTemp, 10));
const total = getTotalX(arr, brr);
ws.write(total + '\n');
ws.end();
}
풀고보니까 진짜 간단한 문제였는데 며칠을 고민하고 헤맸다ㅠ
왜냐구요? every 생각을 못해서...^^
mdn을 계속 들여다봤는데도 왜 every 보고도 그냥 지나친걸까?
숫자만 계산해서 리턴하면 되는건데!!!
every는 주어진 조건을 전부다 통과한다면 true, 아니라면 false를 뱉는다.
a배열에서 가장 큰 수와, b배열에서 가장 작은 수를 찾은 뒤
반복문 속에서 a배열로 나눴을 때, b배열에서 나눴을 때 둘다 나머지가 0인 것들이 나올때마다 count를 해줬다!
해결!