https://www.acmicpc.net/problem/2702
2702번: 초6 수학
첫째 줄에 테스트 케이스의 개수 T(1<=T<=1,000)가 주어진다. 각 테스트 케이스는 두 정수 a와 b로 이루어져 있고, 공백으로 구분되어 있다. (1 <= a,b <= 1,000)
www.acmicpc.net
- 사용언어 : node.js
- 알고리즘 : 수학, 구현 ,사칙연산
- Solved.ac Tier : Bronze I
node.js 코드
1. 문제 정리
테스트 케이스와 정수를 주고 최소 공배수와 최대 공약수를 차례대로 뽑아내면 되는 간단한 문제이다.
최소 공배수 문제가 실버 5인데 이게 도대체 왜 브론즈 1인지 이해가 되지 않지만...
Euclid 알고리즘을 화살표 함수로 만들어서 간단히 해결 해보았다.
let Euclid = (x, y) =>{
if (y === 0)
return x;
else
return Euclid(y, x%y);
}
2. 완성 코드
let inputs = require('fs').readFileSync('/dev/stdin').toString().trim().split('\n'); // /dev/stdin
let Euclid = (x, y) =>{
if (y === 0)
return x;
else
return Euclid(y, x%y);
}
for(let i = 1; i<=inputs[0]; i++){
let input = inputs[i].split(' ')
let ans = Euclid(input[0], input[1])
console.log((parseInt(input[0]) * parseInt(input[1]))/ ans, ans)
}