作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
说明
找出矩形的最小周长及其尺寸。当该区域不是一个完美的正方形时,我不确定如何在 python 中实现它。
样本输入
单位面积
100
15
195
样本输出
Minimum perimeter is 40 with dimensions 10 x 10
Minimum perimeter is 16 with dimensions 3 x 5
Minimum perimeter is 56 with dimensions 13 x 15
我的密码
import math
zero = False
while not zero:
newInput = int(input())
if newInput == 0:
zero = True
else:
l = math.sqrt(newInput)
sq = l * l
if sq == l:
print('Minimum perimeter is ' + l + ' with dimensions ' + l + ' x ' + l)
else:
# I don't know how to find minimum perimeter and its dimensions, when the integer is not a perfect square
最佳答案
从面积的平方根开始,搜索下一个能平均划分面积的最小整数。将面积除以这个得到对应的宽度。然后添加2l + 2w
获取边界。
while True:
newInput = int(input())
if newInput == 0:
break
else:
l = floor(math.sqrt(newInput))
while newInput % l ! = 0:
l -= 1
w = newInput / l
print('Minimum perimiter is %.2f with dimensions %.2f x %.2f' % (2 * l + 2 * w, l, w))
关于python - 找出矩形的最小可能周长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62885850/
好吧,这不起作用!有什么问题呢。有人可以向我指出吗?我已经盯着它看了好几个小时了。我之前寻求过帮助,他提出了结构,但我并没有真正理解它,所以它不起作用。 没有得到正确的计算。我认为读取文件时可能出现问
我需要显示一个圆周。为了做到这一点,我认为我可以为很多 x 计算 y 的两个值,所以我做了: import sympy as sy from sympy.abc import x,y f = x**2
3个不同的类 1个用于处理Circle实例,1个用于Square实例,第3个用于比较之间他们(main) 。在 main 函数中,我找到了 circle (在 c1..c4 之间)和 square (
我是一名优秀的程序员,十分优秀!