유클리드 호제법 예제 - 1
Last updated
Last updated
gcd(a, b):
if b가 0:
a가 최대 공약수
else:
gcd(작은 수, 큰 수 % 작은 수)
t(테스트 케이스)
for t 반복:
a(1번째 수) b(2번째 수)
출력(a * b / gcd(a, b))t = int(input())
def gcd(a, b):
if b == 0:
return a
else:
return gcd(b, a % b)
ans = []
for _ in range(t):
a, b = map(int, input().split())
result = a * b // gcd(a, b)
ans.append(str(result))
print("\n".join(ans))import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
StringBuilder sb = new StringBuilder();
for (int i = 0; i < t; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int result = a * b / gcd(a, b);
sb.append(result).append("\n");
}
System.out.println(sb);
}
private static int gcd(int a, int b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
}