给定一个以米为单位的区域,反复移除最大可能的正方形。

3

我正在学习Python,并想解决一个问题,该问题需要将面积作为整数输入,并计算可以用它制作多少平方米。

示例

例如,如果您输入12平方米的面积(输入12),则可以制作一个3x3平方米的正方形(面积为9平方米)。这将使您剩下3平方米的面积,因此您可以将它们转换为三个1x1平方米的正方形。

示例输入和输出。

input: function(12)
output: 9,1,1,1

input: function(22)
output: 16, 4, 1, 1

input: function(15324)
output: 15129,169,25,1

我尝试了以下方法,但无法完全实现。
def area(num):
    return num * num

number = float(input(" Please Enter any numeric Value : "))

area= square(number)

print(area)

我只尝试返回一个给定数字的平方数,但是基于这个问题,我该如何进行改进?


你能提供更多关于问题的信息吗? - Cardstdani
只要输入一个数字,它就会返回可以用它制作多少平方米。例如,如果您输入12,则可以制作3x3并剩余3。因此,您将返回3x3的乘积为9,并将余数返回为1x1。即9、1、1、1。 - Teshie Ethiopia
1
我认为应该指出,“计算可以用它制作多少平方米”并不完全准确——结果既不是可以制作的最少正方形,也不是可以制作的最多正方形。例如,使用“4,4,4”可以制作出比您的示例更少的总正方形数。您似乎正在寻找贪心地删除最大可能整数平方的特定过程的结果。 - kcsquared
4个回答

1
您可以使用 while 循环来完成此操作:
import numpy as np


def func(a):
    lst = []
    while a: # Repeat until a reaches 0
        n = int(np.sqrt(a))**2 # Largest square number < a
        a -= n # Reduce a by n
        lst.append(n) # Append n to a list and repeat
    return lst

print(func(12))
print(func(22))
print(func(15324))

输出:

[9, 1, 1, 1]
[16, 4, 1, 1]
[15129, 169, 25, 1]

1

我认为您离解决方案已经很近了。我实现了一个简单但有效的解决方案。


import math

def square(n):
    lis = []
    
    rest = n
    while n > 0:
        rest = math.floor(math.sqrt(n))**2
        lis.append(rest)
        n-= rest
    return(lis)

你可以看到,这行代码 math.floor(math.sqrt(n))^2 是计算比 n 小的最接近完全平方数。然后,我将结果保存在一个列表中,并通过减去余数更新 n 的值。通过 while 循环迭代此过程,我得到了一个包含你指定结果的最终列表。
square(12)
>>> [9, 1, 1, 1]

square(22)
>>> [16, 4, 1, 1]

square(15324)
>>> [15129, 169, 25, 1]

编辑
输入命令以执行该函数。

n = float(input(" Please Enter any numeric Value : "))
square(n)

总之,如果您想要一个具有提示功能的全能函数。

import math

def square_prompt():
    n = 1
    while n>0:
        n = float(input(" Please Enter any numeric Value : "))
        if n:
            print(square(n))

square_prompt()

注意:如果您输入的数字小于等于0,将会停止循环(这意味着用户没有其他数字可供程序询问)。


这是最有效的答案,但在运行代码时我遇到了一个错误 NameError: name 'room' is not defined。你能提供一个带有输入提示的可工作代码吗?谢谢。 - Teshie Ethiopia
抱歉,我不小心更改了返回值的变量名称。现在,我已经修复了代码并在结尾添加了提示部分。如果您对代码有其他问题,请告诉我。 - Andrea Ierardi

1
from math import sqrt

def square(n):
    lst = [] # Empty List
    while True: 
        for a in range(1,int(n)): 
            if n-a > 0:
                 if round(sqrt(n-a)) == sqrt(n-a):
                    lst.append(str((round(n-a)))) # I am check that if round of square of a number is equal to square of number because sqrt gives square for every not but I need only perfect square. Square Of 3 is 1.732 but this is a perfect square not then round(1.732) returns 2 which is not equal to 1.732.

                    n = a 
        if n==4: # This line is because 4-Anything is not a perfect square therefore I need to add a custom if statement. 
            lst.append(str(4)) # Because 4 is a perfect square of therefore I add 4 to the list.
            n = 0
            break # exit While Loop 
        if n<=4: # Because if n is 3 then I need to add 1 to the list for n times.
            break # exit While loop
    for a in range(int(n)): # add 1 to n times in lst if n is less than 4.
        lst.append('1')
    return ' '.join(lst)

def area(num):
    return num * num

number = float(input(" Please Enter any numeric Value : "))

area= square(number)

print(area)

输出:


IN:22, OUT:16 4 1 1

这是我可以为您解释的内容。

n -= n-a is just n = a - Nin17
这个可以正常工作,但你能解释一下你的答案吗? - Teshie Ethiopia
@TeshAych 是的,我可以解释一下。你可以等2分钟后看一下。 - codester_09
@TeshAych 你现在可以检查了。我会尽可能解释清楚。 - codester_09

-1
你可以将这个问题转化为一个“找到最接近的平方数”的问题,因此你需要计算输入面积的平方根,并使用“math”库向下取整该值,得到最接近的平方数。
import math

area = float(input(" Please Enter any numeric Value : "))
sqrt = area**0.5
closest = math.floor(sqrt)**2
print(closest, area-closest)

输出:

Please Enter any numeric Value : 12
9 3.0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接