1. 최소공배수 = A * B / 최대공약수
2. 오버플로우를 조심해라
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.io.*; | |
/** | |
* https://www.acmicpc.net/problem/5347 | |
* BOJ 백준온라인져지 5347 LCM 풀이 | |
*/ | |
public class Main { | |
private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); | |
public static void main (String args[]) throws IOException { | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
int N = Integer.parseInt(br.readLine()); | |
for (int i = 0; i < N; i++) { | |
String str1[] = br.readLine().split(" "); | |
Long A = Long.parseLong(str1[0]); | |
Long B = Long.parseLong(str1[1]); | |
bw.write((A * B / gcm(A, B)) + "\n"); | |
} | |
bw.flush(); | |
} | |
private static long gcm (long a, long b) { | |
long mod; | |
while ((mod = a % b) > 0) { | |
a = b; | |
b = mod; | |
mod = a % b; | |
} | |
return b; | |
} | |
} |
문제
두 수 a와 b가 주어졌을 때, a와 b의 최소 공배수를 구하는 프로그램을 작성하시오.
입력
첫째 줄에 테스트 케이스의 개수 n이 주어진다. 다음 n개 줄에는 a와 b가 주어진다. a와 b사이에는 공백이 하나 이상 있다. 두 수는 백만보다 작거나 같은 자연수이다.
출력
각 테스트 케이스에 대해서 입력으로 주어진 두 수의 최소공배수를 출력한다.
'IT > 알고리즘' 카테고리의 다른 글
BOJ 백준온라인져지 6986 절사평균 풀이 (0) | 2018.04.18 |
---|---|
BOJ 백준온라인져지 14488 준오는 급식충이야!! 풀이 (1) | 2018.04.17 |
BOJ 백준온라인져지 2532 반도체 설계 풀이 (1) | 2018.04.11 |
BOJ 백준온라인져지 8595 히든 넘버 풀이 (0) | 2018.04.10 |
BOJ 백준온라인져지 8741 이진수 풀이 (0) | 2018.04.10 |