https://www.acmicpc.net/problem/2609
문제
두 개의 자연수를 입력받아 최대 공약수와 최소 공배수를 출력하는 프로그램을 작성하시오.
입력
첫째 줄에는 두 개의 자연수가 주어진다. 이 둘은 10,000이하의 자연수이며 사이에 한 칸의 공백이 주어진다.
출력
첫째 줄에는 입력으로 주어진 두 수의 최대공약수를, 둘째 줄에는 입력으로 주어진 두 수의 최소 공배수를 출력한다.
풀이
최대 공배수를 유클리드 호제법을 이용하여 구한다.
최소 공배수는 (두 수의 곱)/(두 수의 최대 공약수) 인 것을 이용하여 구한다.
코드
#include <string>
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <climits>
#include <set>
#define p(a, b) make_pair(a, b)
#define SWAP(a, b, type) do { \
type temp; \
temp = a; \
a = b; \
b = temp; \
} while (0)
#define INF 1000000000
#define endl '\n'
#define ll long long
using namespace std;
int a,b;
int GCD(int a,int b){
int r=a%b;
while(r!=0){
a=b;
b=r;
r=a%b;
}
return b;
}
void input() {
cin>>a>>b;
if(a<b) SWAP(a,b,int);
}
void init() {
}
void solution() {
int gcd=GCD(a,b);
cout<<gcd<<endl;
cout<<a*b/gcd<<endl;
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
input();
solution();
return 0;
}
'백준 > Math' 카테고리의 다른 글
[BOJ] 1629 곱셈 (0) | 2022.03.20 |
---|---|
[BOJ] 9375 패션왕 신해빈 (0) | 2022.03.18 |
[BOJ] 20943 카카오톡 (0) | 2022.02.20 |