迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > C++ >

C++ 中的 std::gcd 函数

作者:迹忆客 最近更新:2024/01/02 浏览次数:

本文将解释如何在 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

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Arduino 中停止循环

发布时间:2024/03/13 浏览次数:444 分类:C++

可以使用 exit(0),无限循环和 Sleep_n0m1 库在 Arduino 中停止循环。

Arduino 复位

发布时间:2024/03/13 浏览次数:315 分类:C++

可以通过使用复位按钮,Softwarereset 库和 Adafruit SleepyDog 库来复位 Arduino。

Arduino 的字符转换为整型

发布时间:2024/03/13 浏览次数:181 分类:C++

可以使用简单的方法 toInt()函数和 Serial.parseInt()函数将 char 转换为 int。

Arduino 串口打印多个变量

发布时间:2024/03/13 浏览次数:381 分类:C++

可以使用 Serial.print()和 Serial.println()函数在串口监视器上显示变量值。

Arduino if 语句

发布时间:2024/03/13 浏览次数:123 分类:C++

可以使用 if 语句检查 Arduino 中的不同条件。

Arduino ICSP

发布时间:2024/03/13 浏览次数:214 分类:C++

ICSP 引脚用于两个 Arduino 之间的通信以及对 Arduino 引导加载程序进行编程。

使用 C++ 编程 Arduino

发布时间:2024/03/13 浏览次数:127 分类:C++

本教程将讨论使用 Arduino IDE 在 C++ 中对 Arduino 进行编程。

Arduino 中的子程序

发布时间:2024/03/13 浏览次数:168 分类:C++

可以通过在 Arduino 中声明函数来处理子程序。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便