以三角形排列硬币时获得最大高度的 Python 程序
在本文中,我们将了解如何解决以下问题陈述。
问题陈述 − 给定 N 枚硬币,我们需要将其排列成三角形的形式,即第一行有 1 枚硬币,第二行有 2 枚硬币,依此类推,我们需要显示 N 枚硬币可以达到的最大高度。
现在让我们观察以下实现中的解决方案 −
示例
# squareroot def squareRoot(n): # initial approximation x = n y = 1 e = 0.000001 # allowed error while (x - y > e): x = (x + y) / 2 y = n/x return x # max height def find(N): # calculating portion of the square root n = 1 + 8*N maxH = (-1 + squareRoot(n)) / 2 return int(maxH) # main N = 17 print("Maximum height is :",find(N))
输出
Maximum height is : 5
所有变量均在局部作用域中声明,并且在上图中可以看到它们的引用。
Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.
结论
在本文中,我们学习了如何制作以三角形排列硬币时获得最大高度的 Python 程序。
广告