- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我自己无法解决以下问题,所以我搜索并找到了一个可行的算法。有人可以向我解释该算法背后的思维过程、数学或其他内容吗?
类型:https://www.codewars.com/kata/square-into-squares-protect-trees/train/python
def decompose(n):
goal = 0
result = [n]
while result:
current = result.pop()
goal += current ** 2
for i in range(current - 1, 0, -1):
if goal - (i ** 2) >= 0:
goal -= i ** 2
result.append(i)
if goal == 0:
result.sort()
return result
return None
edit: I'm new to Python and had some trouble understanding the code. It's clear to me now.
最佳答案
让我们让程序自己解释:
def decompose(n):
goal = 0
print "Adding n, %s, to sequence.\n" % (n)
result = [n]
while result:
current = result.pop()
print "The last number, %s, wasn't helpful. Removing it from sequence and adding it back to `goal`" % (current)
print "Trying lower numbers now.\n" if current - 1 > 0 else "\n"
goal += current ** 2
for i in range(current - 1, 0, -1):
print "Trying %s" % (i)
if goal - (i ** 2) >= 0:
goal -= i ** 2
result.append(i)
"This number, %s, might work. Goal is not below zero. Adding it to sequence and subtracting from `goal`." % (i)
if goal == 0:
result.sort()
print "\nFound result, %s." % (result)
return result
else:
print "This number, %s, is too big here. Continuing." % (i)
return None
产生:
> decompose(10)
Adding n, 10, to sequence.
The last number, 10, wasn't helpful. Removing it from sequence and adding it back to `goal`
Trying lower numbers now.
Trying 9
Trying 8
This number, 8, is too big here. Continuing.
Trying 7
This number, 7, is too big here. Continuing.
Trying 6
This number, 6, is too big here. Continuing.
Trying 5
This number, 5, is too big here. Continuing.
Trying 4
Trying 3
This number, 3, is too big here. Continuing.
Trying 2
This number, 2, is too big here. Continuing.
Trying 1
The last number, 1, wasn't helpful. Removing it from sequence and adding it back to `goal`
The last number, 4, wasn't helpful. Removing it from sequence and adding it back to `goal`
Trying lower numbers now.
Trying 3
Trying 2
Trying 1
The last number, 1, wasn't helpful. Removing it from sequence and adding it back to `goal`
The last number, 2, wasn't helpful. Removing it from sequence and adding it back to `goal`
Trying lower numbers now.
Trying 1
The last number, 1, wasn't helpful. Removing it from sequence and adding it back to `goal`
The last number, 3, wasn't helpful. Removing it from sequence and adding it back to `goal`
Trying lower numbers now.
Trying 2
Trying 1
The last number, 1, wasn't helpful. Removing it from sequence and adding it back to `goal`
The last number, 2, wasn't helpful. Removing it from sequence and adding it back to `goal`
Trying lower numbers now.
Trying 1
The last number, 1, wasn't helpful. Removing it from sequence and adding it back to `goal`
The last number, 9, wasn't helpful. Removing it from sequence and adding it back to `goal`
Trying lower numbers now.
Trying 8
Trying 7
This number, 7, is too big here. Continuing.
Trying 6
Found result, [6, 8].
=> [6, 8]
关于python - 套路问题 : square-into-squares-protect-trees,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54079405/
我一直在努力学习 TensorFlow,我注意到不同的函数用于相同的目标。例如,为了平方变量,我看到了 tf.square()、tf.math.square() 和 tf.keras.backend.
我正在创建一个应用程序,我需要在其中显示平方根符号和数字。例如 /¯¯¯¯¯¯¯¯¯¯¯ \/ (v^2) / T 我也希望显示数字的平方。任何人都可以帮助显示此类文本。此文本来自数据库并且可以
我自己无法解决以下问题,所以我搜索并找到了一个可行的算法。有人可以向我解释该算法背后的思维过程、数学或其他内容吗? 类型:https://www.codewars.com/kata/square-in
是否可以使用 Square Connect API 访问有关商家客户的任何信息?最理想的信息是客户为收据输入的电子邮件地址,但某些类型的唯一客户 ID 可以很好地确定回头客。 查看 Square Co
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我正在尝试使用 Font Awesome 堆叠来实现这一点 我试过两种方法,但都很丑。问题是 fa-square 和 fa-square-o 大小不同,所以当你堆叠它们时,它们不会对齐! my fid
这个惊人的code golf answer to 这个数字是 Loeschian 吗?整体而言: Python, 49 bytes lambda n:0in[(n-3*i*i+0j)**.5%1for
这个问题在这里已经有了答案: Is floating point math broken? (31 个回答) 去年关闭。 #include #include using namespace std
我有一个名为Complex的类,它具有实数和虚数的访问器方法,以及将复数表示为字符串的方法和获取复数大小的方法。我在实现最后三个方法 square()(对复数求平方)、modulusSquared()
有没有办法使用 Square Connect API 获取通过网络钩子(Hook)发送的付款 ID 并找到关联的订单 ID? 我发现文档中列出的付款对象 (https://docs.connect.s
你可以有一个父类(super class) Shape,Square 和 Rectangle 是两个子类,但是你可以有 Square 子类 Rectangle,因为 Square 是一个四边相等的特殊
我正在尝试在 Google Search Console 中为 Weebly/Square 网站使用 PageSpeed Insights 并收到错误消息: 灯塔返回错误:NOT_HTML。提供的页面
我有一个包含 A 列和 B 列的数据框。我想创建第三列,它是两列平方和的平方根。 以下是我的示例数据: A B 10 7 10 8 9 8 10 11 13 5 3 0 12 8
给定以下二维点: 213 106.8 214 189 214 293.4 213 324 223 414 我想找到穿过它们的最小二乘垂直轴线的方程。我的计划是得到一个线方程,这样我就可以测试后续点到最
我的 WPF 项目中有一个 UniformGrid 对象,它有 2 行和 3 列,其宽度和高度设置为自动(两个对齐方式都设置为拉伸(stretch))。 这个网格将容纳 6 个正方形,我想尽可能多地填
我发布此问题是因为,我的代码没有在正确的位置停止迭代。谁能让我确定哪里出了问题? 一切工作正常(我一直认为这是现实中的错误。) 问题: 1)在每次迭代中,我都在最小化不幸的是,它没有正确地最小化(非常
“方 block ” 输入 : 棋盘大小 m × n (m, n ∈ {1,...,32}),三元组列表 (i, j, k) , 其中 i ∈ {1,...,m}, j ∈ {1,...,n}, k
我正在尝试为 Square Connect API 开发一个包装器。我正在寻找沙盒帐户或将测试数据导入新帐户的方法,以便我可以快速开始开发方面的工作。 谢谢! 最佳答案 为了供从 Google 登陆这
我有一个任务,我发现它非常困难。任何帮助将不胜感激。 通过创建 Shape 类 Circle、Square 和 Triangle 来构建层次结构。对于这些派生类,创建默认构造函数和构造函数,其参数可以
题目地址: https://leetcode.com/problems/maximal-square/description/ 题目描述 Given a 2D binary matrix fill
我是一名优秀的程序员,十分优秀!