代码之家  ›  专栏  ›  技术社区  ›  Tesh Aych

用x乘x米查找米圆的面积,并返回余数

  •  0
  • Tesh Aych  · 技术社区  · 2 年前

    所以我在学习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)
    

    我只试图返回给定数字的平方数,但如何根据问题改进它?

    1 回复  |  直到 2 年前
        1
  •  1
  •   Andrea Ierardi    2 年前

    我认为你已经接近解决方案了。 我实施了一个简单但有效的解决方案。

    
    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减去其余部分来更新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)
    

    总之,如果你想有一个内部有提示的all-In函数。

    
    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的数字,您将停止循环(这意味着用户没有任何其他数字可向程序请求)。

        2
  •  1
  •   Nin17    2 年前

    您可以使用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]
    
        3
  •  1
  •   Sharim09    2 年前
    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
    

    这是我可以向你解释的。

        4
  •  -1
  •   Cardstdani    2 年前

    您可以将此问题转换为 find the closest square number 问题,因此您需要计算输入面积的平方根,并使用 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