ในบทช่วยสอนนี้ เราจะพูดถึงโปรแกรมค้นหา GCD ของตัวเลขทศนิยม
สำหรับสิ่งนี้เราจะได้รับจำนวนเต็มสองจำนวน งานของเราคือการหา GCD (ตัวหารร่วมที่ยิ่งใหญ่ที่สุด) ของจำนวนเต็มทั้งสองที่ให้มา
ตัวอย่าง
#include <bits/stdc++.h>
using namespace std;
//returning GCD of given numbers
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