首页 文章资讯内容详情

程序在C ++中找到浮点数的GCD

2026-06-04 1 花语

在本教程中,我们将讨论查找浮点数的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