C++ 中的 std::gcd 函数

Jinku Hu 2023年10月12日
  1. 使用 std::gcd 函数在 C++ 中计算两个整数的最大公约数
  2. 在 C++ 中使用 std::lcm 函数来计算两个整数的最小公约数
  3. 使用 std::midpoint 函数在 C++ 中计算两个数字的中点
C++ 中的 std::gcd 函数

本文将解释如何在 C++ 中使用 STL 数值库中的 std::gcd 和其他有用的数学函数。

使用 std::gcd 函数在 C++ 中计算两个整数的最大公约数

STL 使用 <algorithm> 头提供了多种算法,但它也提供了强大的数学函数,其中一些可以被认为是数值算法。这些函数是使用标题 - numeric 提供的。

我们将探索计算两个整数的最大公约数的 std::gcd 函数。最大公约数是将每个给定整数相除的最大正整数。

std::gcd 接受两个整数值(mn)并返回 |m||n| 的最大公约数。如果 mn 碰巧都为零,则该函数也返回零。下面的示例代码演示了 std::gcd 的基本用法,并将相应的结果打印到控制台。

#include <iomanip>
#include <iostream>
#include <numeric>
#include <vector>

using std::cout;
using std::endl;
using std::setw;
using std::vector;

int main() {
  std::vector<std::pair<int, int>> vec = {
      {12125, 1235}, {124, 1122}, {-1235, 321}, {12, 144}};

  for (const auto &item : vec) {
    cout << "Greatest common divisor of " << setw(5) << item.first << " and "
         << setw(5) << item.second << " is "
         << std::gcd(item.first, item.second) << endl;
  }

  return EXIT_SUCCESS;
}

输出:

Greatest common divisor of 12125 and  1235 is 5
Greatest common divisor of   124 and  1122 is 2
Greatest common divisor of -1235 and   321 is 1
Greatest common divisor of    12 and   144 is 1

在 C++ 中使用 std::lcm 函数来计算两个整数的最小公约数

数字库中提供的另一个类似函数是 std::lcm,它计算两个整数的最小公倍数。该函数接受两个类似于 std:gcd 的整数,并返回|m||n|(表示参数)的最小公约数。

请注意,这两个函数都有 constexpr 指定符,这意味着它们可能用于常量表达式,并且该函数也获得了 inline 分类。

#include <iomanip>
#include <iostream>
#include <numeric>
#include <vector>

using std::cout;
using std::endl;
using std::setw;
using std::vector;

int main() {
  std::vector<std::pair<int, int>> vec = {
      {12125, 1235}, {124, 1122}, {-1235, 321}, {12, 144}};

  for (const auto &item : vec) {
    cout << "Least common multiple of " << setw(5) << item.first << " and "
         << setw(5) << item.second << " is "
         << std::lcm(item.first, item.second) << endl;
  }

  return EXIT_SUCCESS;
}

输出:

Least common multiple of 12125 and  1235 is 2994875
Least common multiple of   124 and  1122 is 69564
Least common multiple of -1235 and   321 is 396435
Least common multiple of    12 and   144 is 144

使用 std::midpoint 函数在 C++ 中计算两个数字的中点

std::midpoint 是一个功能强大的函数,用于计算两个给定数字的一半,而用户无需担心溢出。即,如果我们尝试计算两个大整数的一半,它们的总和大于 64 位整数的上限,那么我们就会溢出,结果将是错误的。

例如,以下代码片段试图找到两个整数的中点,其中一个与 uint64_t 中可以存储的最大数相同,另一个小于 10。请注意,使用常用算术运算符的计算会产生错误的数字,而 std::midpoint 返回正确的结果。

请注意,此函数自 C++20 标准以来一直是语言的一部分,可能在旧版本中不可用。std::midpoint 也适用于指针值,但给定的指针应该是同一数组的元素。否则,行为是未定义的。

#include <iomanip>
#include <iostream>
#include <numeric>
#include <vector>

using std::cout;
using std::endl;
using std::setw;
using std::vector;

int main() {
  uint64_t a = std::numeric_limits<uint64_t>::max();
  uint64_t b = std::numeric_limits<uint64_t>::max() - 10;

  cout << "a: " << a << '\n'
       << "b: " << b << '\n'
       << "Incorrect: " << setw(20) << (a + b) / 2 << '\n'
       << "Correct  : " << setw(20) << std::midpoint(a, b) << endl;

  return EXIT_SUCCESS;
}

输出:

a: 18446744073709551615
b: 18446744073709551605
Incorrect:  9223372036854775802
Correct  : 18446744073709551610
作者: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

DelftStack.com 创始人。Jinku 在机器人和汽车行业工作了8多年。他在自动测试、远程测试及从耐久性测试中创建报告时磨练了自己的编程技能。他拥有电气/电子工程背景,但他也扩展了自己的兴趣到嵌入式电子、嵌入式编程以及前端和后端编程。

LinkedIn Facebook