문제) 백준 - 수학 - 적어도 대부분의 배수
https://www.acmicpc.net/problem/1145
1145번: 적어도 대부분의 배수
첫째 줄에 다섯 개의 자연수가 주어진다. 100보다 작거나 같은 자연수이고, 서로 다른 수이다.
www.acmicpc.net
자연수가 5개만 주어지므로 삼중 반복문을 통해 최소 공배수를 구하며 가장 작은 것을 찾습니다.
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 1000000000 | |
#define INF 987654321 | |
#define MOD 1001 | |
#define ll long long | |
#define int ll | |
using namespace std; | |
typedef pair<int, int> p; | |
int arr[5]; | |
// a < b | |
int LCM(int a, int b){ | |
if(b % a == 0) return a; | |
return LCM(b % a, a); | |
} | |
int solve(int a, int b){ | |
return a * b / LCM(a, b); | |
} | |
signed main() { | |
ios_base::sync_with_stdio(0); | |
cin.tie(0); cout.tie(0); | |
for(int i = 0; i < 5; ++i) | |
cin >> arr[i]; | |
sort(arr, arr + 5); | |
int ans = INF; | |
for(int i = 0; i < 5; ++i) | |
for(int j = i + 1; j < 5; ++j) | |
for(int k = j + 1; k < 5; ++k) | |
ans = min(ans, solve(solve(arr[i], arr[j]), arr[k])); | |
cout << ans << endl; | |
return 0; | |
} |
반응형
'PS(Problem Solving) > 백준_BOJ' 카테고리의 다른 글
[백준] 20207번 - 달력 (C++) 문제 및 풀이 (0) | 2022.02.14 |
---|---|
[백준] 21939번 - 문제 추천 시스템 Version 1 (C++) 문제 및 풀이 (0) | 2022.02.14 |
[백준] 1633번 - 최고의 팀 만들기 (C++) 문제 및 풀이 (0) | 2022.02.12 |
[백준] 4803번 - 트리 (C++) 문제 및 풀이 (0) | 2022.02.12 |
[백준] 3184번 - 양 (C++) 문제 및 풀이 (0) | 2022.02.12 |
댓글