BOJ::1934 최소공배수
https://www.acmicpc.net/problem/1934
최소공배수 구하는 문제.
유클리드 호제법을 이용하면 쉽게 정답을 구할 수 있다.
<JAVA>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | 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 NumberFormatException, IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); StringTokenizer st = null; for(int i = 0 ; i < n ; i++){ st = new StringTokenizer(br.readLine()); int x = Integer.parseInt(st.nextToken()); int y = Integer.parseInt(st.nextToken()); System.out.println((x*y)/getGcd(x,y)); } } //최대공약수 구하기 public static int getGcd(int x,int y){ if(y>x){ int tmp=x; x=y; y=tmp; } while(x%y!=0){ int t = x%y; x=y; y=t; } return y; } } | cs |
<C++>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #include <iostream> using namespace std; int n; int getGcd(int x, int y) { if (y > x) { int tmp = x; x = y; y = tmp; } while (x%y != 0) { int t = x % y; x = y; y = t; } return y; } int main() { cin >> n; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; cout << (x*y) / getGcd(x, y) << endl; } } | cs |
'BOJ::문제풀이' 카테고리의 다른 글
1987 알파벳 (0) | 2018.01.01 |
---|---|
1965 상자넣기 (0) | 2018.01.01 |
1932 숫자삼각형 (0) | 2018.01.01 |
1929 소수 구하기 (0) | 2018.01.01 |
1920 수 찾기 (0) | 2018.01.01 |