※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※
이번에 볼 문제는 백준 5344번 문제인 GCD이다.
문제는 아래 링크를 확인하자.
https://www.acmicpc.net/problem/5344
5344번: GCD
The first line of input will be a positive integer, n, indicating the number of problem sets. Each problem set consist of two positive integers, separated by one or more spaces. There are no blank lines in the input.
www.acmicpc.net
입력으로 주어지는 두 양의 정수의 최대공약수를 구하는 문제이다.
유클리드 알고리즘(Euclidean algorithm)을 이용해 문제를 해결해주자.
아래는 제출한 소스코드이다.
#include <iostream>
using namespace std;
typedef long long ll;
ll gcd(ll x, ll y) {
if (y == 0) return x;
return gcd(y, x % y);
}
int T;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> T;
while (T--) {
ll x, y; cin >> x >> y;
cout << gcd(x, y) << '\n';
}
}
728x90
'BOJ' 카테고리의 다른 글
[BOJ 10190 // C++] Acronyms (0) | 2022.12.22 |
---|---|
[BOJ 6843 // C++] Anagram Checker (0) | 2022.12.22 |
[BOJ 26590 // C++] Word Mix (0) | 2022.12.22 |
[BOJ 16911 // C++] 그래프와 쿼리 (0) | 2022.12.22 |
[BOJ 26564 // C++] Poker Hand (0) | 2022.12.21 |