![[백준 / BOJ] 2609번 최대공약수와 최소공배수 (C++, Python)](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2Fdivu1z%2FbtrJp1InwPN%2FBcbxlNjosVtgKlbqpB8cd1%2Fimg.png)
[백준 / BOJ] 2609번 최대공약수와 최소공배수 (C++, Python)◎ 자료구조와 알고리즘/백준(BOJ) 문제풀이2022. 6. 7. 20:47
Table of Contents
반응형
링크 : https://www.acmicpc.net/problem/2609
2609번: 최대공약수와 최소공배수
첫째 줄에는 입력으로 주어진 두 수의 최대공약수를, 둘째 줄에는 입력으로 주어진 두 수의 최소 공배수를 출력한다.
www.acmicpc.net
문제

문제 풀이
나의 설명보다는 아래 링크를 참고하는것이 훨씬 좋을 것 같다. 유클리드 호제법을 활용하면 된다.
https://sectumsempra.tistory.com/77
[백준2609]-최대공약수와 최소공배수(C++)/유클리드 호제법
https://www.acmicpc.net/problem/2609 2609번: 최대공약수와 최소공배수 첫째 줄에는 입력으로 주어진 두 수의 최대공약수를, 둘째 줄에는 입력으로 주어진 두 수의 최소 공배수를 출력한다. www.acmicpc.net 유
sectumsempra.tistory.com
C++ 코드 전문
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
#include <algorithm> | |
#include <iostream> | |
#include <vector> | |
int gcd(int a, int b) { | |
int c; | |
while (b != 0) { | |
c = a % b; | |
a = b; | |
b = c; | |
} | |
return a; | |
} | |
void init() { | |
std::ios_base::sync_with_stdio(false); | |
std::cin.tie(nullptr); | |
std::cout.tie(nullptr); | |
} | |
int main() { | |
init(); | |
int a, b; | |
std::cin >> a >> b; | |
int res = gcd(a, b); | |
std::cout << res << "\n"; | |
std::cout << (a * b) / res << "\n"; | |
} |
Python 코드 전문
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 sys | |
import math | |
input = sys.stdin.readline | |
a, b = map(int, input().split()) | |
print(math.gcd(a, b)) | |
print(math.lcm(a, b)) |
소감
반응형
'◎ 자료구조와 알고리즘 > 백준(BOJ) 문제풀이' 카테고리의 다른 글
[백준 / BOJ] 2981번 검문 (C++, Python) (0) | 2022.06.09 |
---|---|
[백준 / BOJ] 1934번 최소공배수 (C++, Python) (0) | 2022.06.07 |
[백준 / BOJ] 1037번 약수 (C++, Python) (0) | 2022.06.06 |
[백준 / BOJ] 5086번 배수와 약수 (C++, Python) (0) | 2022.06.06 |
[백준 / BOJ] 1358번 하키 (C++, Python) (0) | 2022.06.05 |
@Reo :: 코드 아카이브
자기계발 블로그