λ°μν
λ¬Έμ
λΆμ A/Bλ λΆμκ° A, λΆλͺ¨κ° BμΈ λΆμλ₯Ό μλ―Ένλ€. Aμ Bλ λͺ¨λ μμ°μλΌκ³ νμ.
λ λΆμμ ν© λν λΆμλ‘ ννν μ μλ€. λ λΆμκ° μ£Όμ΄μ‘μ λ, κ·Έ ν©μ κΈ°μ½λΆμμ ννλ‘ κ΅¬νλ νλ‘κ·Έλ¨μ μμ±νμμ€. κΈ°μ½λΆμλ λ μ΄μ μ½λΆλμ§ μλ λΆμλ₯Ό μλ―Ένλ€.
μ λ ₯
첫째 μ€κ³Ό λμ§Έ μ€μ, κ° λΆμμ λΆμμ λΆλͺ¨λ₯Ό λ»νλ λ κ°μ μμ°μκ° μμλλ‘ μ£Όμ΄μ§λ€. μ λ ₯λλ λ€ μμ°μλ λͺ¨λ 30,000 μ΄νμ΄λ€.
μΆλ ₯
첫째 μ€μ ꡬνκ³ μ νλ κΈ°μ½λΆμμ λΆμμ λΆλͺ¨λ₯Ό λ»νλ λ κ°μ μμ°μλ₯Ό λΉ μΉΈμ μ¬μ΄μ λκ³ μμλλ‘ μΆλ ₯νλ€.
νμ΄
// νμ΄ : https://whkakrkr.tistory.com
#include <iostream>
#include <vector>
#include <set>
using namespace std;
// μ΅λ곡μ½μ
int getGcp(int a, int b) {
return a==0 ? b : getGcp(b%a, a);
}
// μ΅μ곡배μ
int getLcm(int a, int b) {
int gcp = getGcp(a, b);
return a*b/gcp;
}
// μ½λΆ
void getReduced(int &ja, int &mo) {
int gcp = getGcp(ja, mo);
ja /= gcp;
mo /= gcp;
}
int main() {
ios_base::sync_with_stdio(false);
cout.tie(NULL);
cin.tie(NULL);
int a,b,c,d;
cin >> a >> b >> c >> d;
// (1) κ° λΆμ μ½λΆνκΈ°
getReduced(a,b);
getReduced(c,d);
// (2) ν΅λΆνκΈ°
int lcm = getLcm(b,d);
a *= lcm/b;
c *= lcm/d;
// (3) λνκΈ°
int ja = a+c;
int mo = lcm;
// (4) λν΄μ λμ¨ λΆμ μ½λΆ
getReduced(ja, mo);
cout << ja << " " << mo;
return 0;
}
λ°μν
'π Cpp > [BOJ] λ¨κ³λ³λ‘ νμ΄λ³΄κΈ°' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[BOJ][C++] λ°±μ€ 4134λ²: λ€μ μμ (Silver IV) (0) | 2025.01.16 |
---|---|
[BOJ][C++] λ°±μ€ 1934λ²: μ΅μ곡배μ (Bronze I) (0) | 2025.01.13 |
[BOJ][C++] λ°±μ€ 11478λ²: μλ‘ λ€λ₯Έ λΆλΆ λ¬Έμμ΄μ κ°μ (Silver III) (0) | 2025.01.09 |
[BOJ][C++] λ°±μ€ 25305λ²: 컀νΈλΌμΈ (Bronze II) (0) | 2025.01.08 |
[BOJ][C++] λ°±μ€ 24313λ²: μκ³ λ¦¬μ¦ μμ - μ κ·Όμ νκΈ° 1 (Silver V) (0) | 2025.01.07 |