Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 서울사대부고 학원
- c++ 조합
- 백준 10709
- 백준 1049번
- 성북구 학원
- 월곡중학교 학원추천
- OS
- 백준 14246번
- 월곡동 학원추천
- 상월곡역 학원
- 상월곡동 학원
- 월곡중 학원
- 백준 패션왕 신해빈
- 백준 K보다 큰 구간
- 월곡역 학원
- 백준 1049번 기타줄
- 고정 소수점
- C++ 문자열
- 관리형 학원
- 백준 14246번 K보다 큰 구간
- 백준 dfs
- DFS
- 백준 한국이 그리울 땐 서버에 접속하지
- C++ 9996
- 운영체제
- 백준 2309번 일곱 난쟁이
- 백준 9375번 패션왕 신해빈
- 백준 토마토
- c++ split
- C# 병합정렬
Archives
- Today
- Total
키베이루's diary
[C++] 최대 공약수, 최소 공배수 본문
1. 최대 공약수
int gcd(int a, int b)
{
int n;
while (b != 0) {
n = a % b;
a = b;
b = n;
}
return a;
}
2. 최소 공배수
int lcm(int a, int b)
{
return a * b / gcd(a, b);
}
ex)
https://www.acmicpc.net/problem/1735
1735번: 분수 합
첫째 줄과 둘째 줄에, 각 분수의 분자와 분모를 뜻하는 두 개의 자연수가 순서대로 주어진다. 입력되는 네 자연수는 모두 30,000 이하이다.
www.acmicpc.net
#include<iostream>
#include<vector>
#include<algorithm>
#include<stack>
#include<string>
#include<map>
using namespace std;
int gcd(int a, int b)
{
int n;
while (b != 0) {
n = a % b;
a = b;
b = n;
}
return a;
}
int lcm(int a, int b)
{
return a * b / gcd(a, b);
}
int main() {
int a, b, c, d;
cin >> a >> b >> c >> d;
int lc = lcm(b, d);
int re = a * (lc / b) + c * (lc / d);
int rlc = lc;
int rre = re;
//! 최대공약수로 약분
rlc = lc/ gcd(re, lc);
rre = re / gcd(re, lc);
cout << rre << ' ' << rlc;
}
'알고리즘 > 기타' 카테고리의 다른 글
[C++] Unique (1) | 2023.05.09 |
---|---|
[C++] 백준 10709번 기상캐스터 (0) | 2023.01.17 |
[C++] 백준 14246번 K보다 큰 구간 (0) | 2022.12.27 |
[C++] 백준 2309번 일곱 난쟁이 (0) | 2022.12.27 |
[C++] Combination(조합) (0) | 2022.12.26 |
Comments