Algorithm
- GCD ํฉ -
๋ฌธ์
์์ ์ ์ n๊ฐ๊ฐ ์ฃผ์ด์ก์ ๋, ๊ฐ๋ฅํ ๋ชจ๋ ์์ GCD์ ํฉ์ ๊ตฌํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค.
์ ์ถ๋ ฅ ์์
๋ฌธ์ ํ๊ธฐ
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let input = [];
rl.on("line", function (line) {
input.push(
line
.toString()
.split(" ")
.map((el) => +el)
);
}).on("close", function () {
input.shift();
let result = [];
for (let i = 0; i < input.length; i++) {
result.push(findValue(input[i][0], input[i].splice(1)));
}
console.log(result.join("\n"));
process.exit();
});
let findValue = (a, b) => {
let sum = 0;
for (let i = 0; i < a - 1; i++) {
for (let j = i + 1; j < a; j++) {
sum += gcd(b[i], b[j]);
}
}
return sum;
};
let gcd = (x, y) => {
while (y) {
[x, y] = [y, x % y];
}
return x;
};
๋ฐ์ํ
'๊ฐ์ธ๊ณต๋ถ > Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค][Node.js] 9654๋ฒ : ๋๋ถ ํจ๋ ๋ฐ์ดํฐ (0) | 2021.12.30 |
---|---|
[๋ฐฑ์ค][Node.js] 9653๋ฒ : ์คํ์์ฆ ๋ก๊ณ (0) | 2021.12.29 |
[๋ฐฑ์ค][Node.js] 9498๋ฒ : ์ํ ์ฑ์ (0) | 2021.12.26 |
[๋ฐฑ์ค][Node.js] 9375๋ฒ : ํจ์ ์ ์ ํด๋น (0) | 2021.12.24 |
[๋ฐฑ์ค][Node.js] 9325๋ฒ : ์ผ๋ง? (0) | 2021.12.23 |
๋๊ธ