ในบทช่วยสอนนี้ เราจะพูดถึงโปรแกรมหาตัวหารร่วมมาก (GCD) ของจำนวนทศนิยม
เราจะได้รับสองจำนวนเต็ม (ในตัวอย่างเป็นทศนิยม) งานคือหา GCD (ตัวหารร่วมมากที่สุด) ของตัวเลขสองตัวที่ให้มา
ตัวอย่างโค้ด
#include <bits/stdc++.h>
using namespace std;
double gcd(double a, double b) {
if (a < b) return gcd(b, a);
if (fabs(b) < 0.001) return a;
else return gcd(b, a - floor(a / b) * b);
}
int main() {
double a = 1.20, b = 22.5;
cout << gcd(a, b);
return 0;
}
เอาต์พุต
0.3