Java 中的煎饼数13 May 2025 | 5 分钟阅读 在本节中,我们将讨论什么是煎饼数,并使用不同的方法创建Java 程序来查找煎饼数。煎饼数程序经常在 Java 编码面试和学术中出现。 煎饼数煎饼数 Pj 表示使用 j 次切割可以将圆形或煎饼分割成的最大块数。煎饼数也称为懒人割饼数,更正式的名称是中心多边形数。 在数学上,煎饼数 Pj 表示为 ![]() 下图显示了 j = 0 到 j = 5 的煎饼数。 ![]() 煎饼数 Java 程序以下代码显示了使用上述数学公式实现的煎饼数。 文件名: PancakeNumberExample.java 输出 For j = 0, the pancake number is: 1 For j = 1, the pancake number is: 2 For j = 2, the pancake number is: 4 For j = 3, the pancake number is: 7 For j = 4, the pancake number is: 11 For j = 5, the pancake number is: 16 For j = 6, the pancake number is: 22 For j = 7, the pancake number is: 29 For j = 8, the pancake number is: 37 For j = 9, the pancake number is: 46 使用组合计算煎饼数煎饼数也可以通过组合来获得。使用组合,煎饼数定义为 Pj = 0C0 = 1,对于 j = 0 Pj = 1C0 + 1C1 = 1 + 1 = 2,对于 j = 1 对于 j >= 2, Pj = jC0 + jC1 + jC2 因此, 对于 j = 2, P2 = 2C0 + 2C1 + 2C2 = 1 + 2 + 1 = 4 对于 j = 3, P3 = 3C0 + 3C1 + 3C2 = 1 + 3 + 3 = 7 对于 j = 4, P4 = 4C0 + 4C1 + 4C2 = 1 + 4 + 6 = 11 对于 j = 5, P5 = 5C0 + 5C1 + 5C2 = 1 + 5 + 10 = 16 将上述公式合并,对于 j = 0, 1, 和 >= 2,我们得到 Pj = 1,对于 j = 0,以及 Pj = j + 1C2 + 1,对于 j >= 1 因此, P0 = 1 P1 = 1 + 1C2 + 1 = 2C2 + 1 = 1 + 1 = 2 P2 = 2 + 1C2 + 1 = 3C2 + 1 = 3 + 1 = 4 P3 = 3 + 1C2 + 1 = 4C2 + 1 = 6 + 1 = 7 煎饼数:迭代方法让我们来实现上述组合公式来计算煎饼数。 文件名: PancakeNumberExample1.java 输出 For j = 0, the pancake number is: 1 For j = 1, the pancake number is: 2 For j = 2, the pancake number is: 4 For j = 3, the pancake number is: 7 For j = 4, the pancake number is: 11 For j = 5, the pancake number is: 16 For j = 6, the pancake number is: 22 For j = 7, the pancake number is: 29 For j = 8, the pancake number is: 37 For j = 9, the pancake number is: 46 煎饼数:递归方法组合公式也可以通过递归来实现。下面的程序显示了这一点。 文件名: PancakeNumberExample2.java 输出 For j = 0, the pancake number is: 1 For j = 1, the pancake number is: 2 For j = 2, the pancake number is: 4 For j = 3, the pancake number is: 7 For j = 4, the pancake number is: 11 For j = 5, the pancake number is: 16 For j = 6, the pancake number is: 22 For j = 7, the pancake number is: 29 For j = 8, the pancake number is: 37 For j = 9, the pancake number is: 46 |
在本节中,我们将学习什么是 Strontio 数,并创建 Java 程序来检查给定数字是否为 Strontio。Strontio 数程序经常在 Java 编码测试和学术界中出现。Strontio 数 Strontio 数是指四位数字,当乘以……
阅读 3 分钟
这是 Google、Amazon、TCS、Accenture、Flipkart 等顶级 IT 公司面试中经常提出的问题。通过解决问题,人们希望检查应聘者的逻辑能力、批判性思维和解决问题的能力。因此,在本节中,我们将...
阅读 8 分钟
为了确定字符串中相等对的数量,需要找到文本中相同字符出现在不同位置的所有实例。当两个字符相同但出现在不同索引时,一对被认为是 "相等" 的。目标是确定有多少...
5 分钟阅读
杂耍者序列 在数论中,杂耍者序列由从正整数 n 开始的数字组成,其中每个后续项取决于前一项是偶数还是奇数。序列一直持续到达到 1。如何找到杂耍者序列?杂耍者序列是递归定义的...
7 分钟阅读
在过去的几年里,Python 取得了巨大的进步。超过八百万开发人员使用 Java 来开发应用程序。这两种语言都能够执行几乎所有的任务。现在,让我们简要介绍这两种语言,然后理解 Python 相对于...
阅读 3 分钟
对象类的 equals() 方法用于比较两个对象,该方法接受一个对象并将当前对象与该对象进行比较。如果这两个对象的引用相等,则方法返回 true;否则,则不返回。示例 整个以下 Employee 类...
阅读 2 分钟
连接列表是一个简单的信息形状,由节点组成。其中每个节点都是对系列和学科中节点的引用(或指针)。链表本质上是动态的。因为它不像数组。链表不存储...
7 分钟阅读
矩阵操作是计算机科学和编程中的基本方面。在 Java 中,开发人员经常会遇到需要对矩阵执行各种操作的情况。一个有趣的矩阵操作是翻转,为该操作引入随机性可以为您的代码增添独特的维度……
阅读 4 分钟
在本节中,我们将通过不同的方法学习如何使用 Java 查看二叉树的底部视图。在二叉树的底部视图中,我们只打印那些当二叉树...时可见的节点。
5 分钟阅读
Java SortedSet<E> 接口 Java 中的 SortedSet<E> 接口是 Java Collections Framework 的一部分,提供了一组唯一的元素,其中元素按排序顺序存储。它扩展了 Set<E> 接口。它于 Java 2 引入,并且一直是重要的...
阅读9分钟
我们请求您订阅我们的新闻通讯以获取最新更新。
我们提供所有技术(如 Java 教程、Android、Java 框架)的教程和面试问题
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India