首页 > 编程语言 >C++快速求解最大公因数 | gcd库函数

C++快速求解最大公因数 | gcd库函数

时间:2023-02-26 19:44:57浏览次数:40  
标签:__ gcd int namespace include 公因数 库函数

1.介绍

gcd全称 :greatest common divisor

使用 __gcd( int x1, int x2 ) 函数可以高效、迅速得到x1, x2两个数的最大公因数。省去手写底层代码,专注代码逻辑的研究

 

2.注意

__gcd( int x1, int x2 ) 前面是两个下划线

 

3.头文件及命名空间

#include<algorithm>
using namespace std;

 

4.示例

#include<iostream>
#include<algorithm>
using namespace std;
int main(){
    int a,b;
    cin>>a>>b;
    cout<<__gcd(a,b);
    return 0;
}
//输入:
//288 540
//输出:
//36

 

5.优化

#include<iostream>
#include<algorithm>
using namespace std;
inline int gcd(int a,int b) {  
    return __gcd(a,b);
}
int main(){
    int a,b;
    cin>>a>>b;
    cout<<__gcd(a,b);
    return 0;
}
//输入:
//288 540
//输出:
//36
//inline()修饰功能简单的函数能提高时间效率

 

标签:__,gcd,int,namespace,include,公因数,库函数
From: https://www.cnblogs.com/shinnyblue/p/17157432.html

相关文章