문제) 백준 - 정수론 - 물통 문제
https://www.acmicpc.net/problem/14881
14881번: 물통 문제
용량이 a, b 리터인 두 물통이 있다. 이때, 물을 적절히 부어서 정확하게 c리터를 만들 수 있는지 아닌지 구하는 프로그램을 작성하시오. 물은 무한히 많다.
www.acmicpc.net
용량이 a인 물통과 b인 물통으로 용량 c를 만들어야 한다. a와 b로 만들 수 있는 가장 작은 단위의 물통을 생각해보면 최대 공약수임을 알 수 있다. 따라서 a와 b의 최대공약수를 이용해 물통 c를 채울 수 있으면 성공, 아니면 실패인 것이다.
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 <bits/stdc++.h> | |
#define endl "\n" | |
#define MAX 5000001 | |
using namespace std; | |
typedef pair<int, int> p; | |
//최대 공약수 | |
int gcd(int a, int b) { | |
if (a > b) swap(a, b); | |
if (b % a == 0) return a; | |
else return gcd(b % a, a); | |
} | |
bool solve(int a, int b, int c) { | |
//예외 처리 | |
if (c > a && c > b) return false; | |
return c % gcd(a, b) == 0 ? true : false; | |
} | |
signed main() { | |
ios_base::sync_with_stdio(0); | |
cin.tie(0); cout.tie(0); | |
int T; cin >> T; | |
while (T--) { | |
int a, b, c; cin >> a >> b >> c; | |
if (solve(a, b, c)) cout << "YES" << endl; | |
else cout << "NO" << endl; | |
} | |
return 0; | |
} |
반응형
'PS(Problem Solving) > 백준_BOJ' 카테고리의 다른 글
[백준] 20040번 - 사이클 게임 (C++) 문제 및 풀이 (0) | 2021.09.28 |
---|---|
[백준] 11653번 - 소인수 분해 (C++) 문제 및 풀이 (0) | 2021.09.27 |
[백준] 1972번 - 놀라운 문자열 (C++) 문제 및 풀이 (0) | 2021.09.23 |
[백준] 2631번 - 줄세우기 (C++) 문제 및 풀이 (0) | 2021.09.03 |
[백준] 5557번 - 1학년 (C++) 문제 및 풀이 (0) | 2021.07.27 |
댓글