#include <iostream>
#include <numeric> // 包含gcd函数
// 计算两个整数的最大公约数 (Greatest Common Divisor, GCD)
int main() {
int a = 56;
int b = 98;
// 使用std::gcd计算最大公约数
int result = std::gcd(a, b);
std::cout << "The GCD of " << a << " and " << b << " is: " << result << std::endl;
return 0;
}
#include <numeric>
:引入了C++标准库中的<numeric>
头文件,其中包含了std::gcd
函数。std::gcd(a, b)
:这是C++17引入的标准库函数,用于计算两个整数a
和b
的最大公约数(GCD)。int result = std::gcd(a, b);
:调用std::gcd
函数,并将结果存储在变量result
中。std::cout
:输出结果到控制台。这段代码展示了如何使用C++标准库中的std::gcd
函数来计算两个整数的最大公约数。
上一篇:c++ cli
下一篇:c++ tcp
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站