C++ 中的维思序列2025 年 5 月 23 日 | 阅读 9 分钟 引言遵循黄金比例,Wythoff 序列是组合博弈论中用于玩家移动的数学组合。它以 Willem Abraham Wythoff 的名字命名,他根据斐波那契数创建了一个与黄金分割有着奇特关系的序列。在本文中,我们将讨论 Wythoff 序列并提供一篇关于生成它的 C++ 代码的文章。 问题陈述Wythoff 序列由两个交错序列组成,分别称为下 Wythoff 序列和上 Wythoff 序列,表示为 A(n) = floor(n * φ) B(n) = A(n) + n 其中 φ (phi) = (1 + sqrt(5)) / 2 是黄金比例。 我们的目标是使用 C++ 生成并显示 Wythoff 序列的前 N 项。 数学背景Wythoff 序列与黄金比例密切相关,这确保了数字分布良好而不会聚集。序列定义为
这些序列在 Wythoff 游戏和 Nim 等组合游戏中扮演着关键角色。 示例 1让我们看一个 C++ 程序来生成并显示 Wythoff 序列的前 N 项。 输出 Enter the number of terms: 10 Wythoff sequence (first 10 terms): Lower Sequence: 1 3 4 6 8 9 11 12 14 16 Upper Sequence: 2 5 7 10 13 15 18 20 23 26 代码解释
示例 2让我们再举一个例子来说明 C++ 中的Wythoff 序列。 输出 Enter the number of terms: 20 Wythoff sequence (first 20 terms): Lower Sequence: 1 3 4 6 8 9 11 12 14 16 17 19 21 22 24 25 27 29 30 32 Upper Sequence: 2 5 7 10 13 15 18 20 23 26 28 31 34 36 39 41 44 47 49 52 Enter a number to search in the sequence: 23 23 is present in the sequence. Sequence saved to wythoff_sequence.txt Wythoff 序列的应用
示例 3让我们再举一个例子来说明 C++ 中的Wythoff 序列。 输出 Enter the number of terms for Wythoff sequence: 30 Sequence generated in 6 ms. ===== Wythoff Sequence Generator ===== 1. Generate & Display Sequence 2. Search for a Number in the Sequence 3. Save Sequence to a File 4. Visualize Sequence 5. Exit ====================================== Enter your choice: 1 Wythoff sequence (first 30 terms): Lower Sequence: 4 19 1 40 3 45 6 8 9 11 12 46 48 14 16 17 21 22 24 25 27 29 30 32 33 35 37 38 42 43 Upper Sequence: 7 31 2 65 5 73 10 13 15 18 20 75 78 23 26 28 34 36 39 41 44 47 49 52 54 57 60 62 68 70 ===== Wythoff Sequence Generator ===== 1. Generate & Display Sequence 2. Search for a Number in the Sequence 3. Save Sequence to a File 4. Visualize Sequence 5. Exit ====================================== Enter your choice: 2 Enter number to search: 39 39 is in the sequence. ===== Wythoff Sequence Generator ===== 1. Generate & Display Sequence 2. Search for a Number in the Sequence 3. Save Sequence to a File 4. Visualize Sequence 5. Exit ====================================== Enter your choice: 2 Enter number to search: 43 43 is in the sequence. ===== Wythoff Sequence Generator ===== 1. Generate & Display Sequence 2. Search for a Number in the Sequence 3. Save Sequence to a File 4. Visualize Sequence 5. Exit ====================================== Enter your choice: 2 Enter number to search: 1000 1000 is NOT in the sequence. ===== Wythoff Sequence Generator ===== 1. Generate & Display Sequence 2. Search for a Number in the Sequence 3. Save Sequence to a File 4. Visualize Sequence 5. Exit ====================================== Enter your choice: 3 Sequence saved to wythoff_sequence.txt ===== Wythoff Sequence Generator ===== 1. Generate & Display Sequence 2. Search for a Number in the Sequence 3. Save Sequence to a File 4. Visualize Sequence 5. Exit ====================================== Enter your choice: 4 ASCII Representation of Wythoff Sequence: 1: **** (4) 2: ******************* (19) 3: * (1) 4: **************************************** (40) 5: *** (3) 6: ********************************************* (45) 7: ****** (6) 8: ******** (8) 9: ********* (9) 10: *********** (11) 11: ************ (12) 12: ********************************************** (46) 13: ************************************************ (48) 14: ************** (14) 15: **************** (16) 16: ***************** (17) 17: ********************* (21) 18: ********************** (22) 19: ************************ (24) 20: ************************* (25) 21: *************************** (27) 22: ***************************** (29) 23: ****************************** (30) 24: ******************************** (32) 25: ********************************* (33) 26: *********************************** (35) 27: ************************************* (37) 28: ************************************** (38) 29: ****************************************** (42) 30: ******************************************* (43) ===== Wythoff Sequence Generator ===== 1. Generate & Display Sequence 2. Search for a Number in the Sequence 3. Save Sequence to a File 4. Visualize Sequence 5. Exit ====================================== Enter your choice: 5 Exiting program... 结论总之,Wythoff 序列优雅地阐释了数论和组合数学之间的相互作用。通过利用黄金比例,可以创建具有实际应用的特殊序列。此处提供的 C++ 实现有效地计算了序列,使得试验不同的 N 值变得容易。 下一主题判断数组是否形成山谷 - C++ |
在本文中,我们将讨论如何将整个 ASCII 文件读入 C++ std::string。在进行实现之前,我们必须了解 C++ 中的 ASCII 文件。什么是 ASCII 文件?转换为 ASCII 格式的文件允许数据导入……
阅读 2 分钟
在 C++ 中,一个数字的数字既不严格递增也不严格递减,则称为“弹跳数”。例如,134468 递增,987654 递减,而 155349 弹跳,它同时表现出这两种趋势。位数少于 100 的数字不会弹跳。一个数字可以被认为是...
5 分钟阅读
在本文中,我们将讨论 C++ 的居中九角数程序。但在其实现之前,我们必须了解 C++ 中的居中九角数。什么是居中九角数?表示有 K 个点的中心九边形的数字称为...
阅读 4 分钟
在本文中,我们将讨论 C++ 中的 std::launder 方法及其语法和示例。什么是 C++ 中的 std::launder() 函数? launder 函数在 C++17 中引入。它是一个与指针来源和基于类型的别名优化相关的实用函数。当有一个名为...
阅读 4 分钟
basic_istream::unget() 函数用于 unget 字符,该函数还会将位置减去一个字符,并允许重用已检索的字符。应提供适当的头文件。使用 basic_istream::unget() 方法的目的是将字符返回到...
阅读 2 分钟
Grundy 数,也称为 Nim 数,对于解决 C++ 中的组合游戏论问题至关重要。它们代表游戏中位置的最小排除 (mex) 值,确定获胜或失败状态。通过计算 Grundy 数,玩家可以预测最佳走法并分析游戏...
7 分钟阅读
二分图定义二分图由于其独特的性质和在实际问题解决场景中的应用,在各个领域都具有重要的意义。以下是对其主要性质、应用及其在不同领域中的含义的探讨:二分图的性质 2-可着色性:二分图的一个基本性质是它...
阅读 15 分钟
类模板的实现为所支持的元素类型提供了极大的灵活性。您可以根据给定的规范,在向量和列表之间交换某些元素,用倒数第三个值替换特定元素。此外,两个向量中的范围可以...
阅读 4 分钟
简介 本文的主要主题是 C++ 中的 std::exponential_distribution 类,它是标准库中用于生成指数分布随机数的相当有用的工具。当关注泊松过程中事件之间的时间时,这种分布很有应用价值……
阅读 6 分钟
获取对象地址的一种安全方法是使用 std::to_address 实用函数,该函数已添加到 C++17 的 C++ 标准库中,无论它是智能指针的实例还是容器的元素。在 C++ 中,获取地址……
阅读 4 分钟
我们请求您订阅我们的新闻通讯以获取最新更新。
我们提供所有技术(如 Java 教程、Android、Java 框架)的教程和面试问题
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India