그 밖의 방법들

[nodeJS] 백준 17433: 신비로운 수

치킨먹고싶어요 2022. 5. 27. 10:03

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

 

17433번: 신비로운 수

첫째 줄에 테스트 케이스의 개수 T가 주어진다. 각 테스트 케이스는 두 줄로 이루어져 있고, 첫째 줄에 N, 둘째 줄에 N개의 정수가 주어진다.

www.acmicpc.net

 

const input = require('fs').readFileSync('/dev/stdin').toString().trim().split('\n');
for (let i = 0; i < input.length; i++) input[i] = input[i].split(/\s/).map(v=>+v);
 
const gcd = (x , y) => {
    while (y) {
        let t = x % y;
        x = y;
        y = t;
    }
    return x;
}
let tmp, res;
for (let i = 1; i <= input[0][0]; i++) {
    tmp =  Array.from({length: input[2 * i - 1][0]}, (undefined, i) => 0);
    for (let j = 1; j < input[2 * i - 1][0]; j++) tmp[j] = Math.abs(input[2 * i][j] - input[2 * i][j - 1]);
    res = tmp[0];
    for (let j = 1; j < input[2 * i - 1][0]; j++) res = gcd(res, tmp[j]);
    console.log(res = (res ? res : "INFINITY"));
}
cs