革命尚未成功

————同志仍需努力————

shinnyblue

导航

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

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()修饰功能简单的函数能提高时间效率

 

posted on 2023-02-26 19:38  ShinnyBlue  阅读(955)  评论(0编辑  收藏  举报

Live2D