gpt4 book ai didi

algorithm - 采访 : draw shapes (circles & squares) until area is less than X

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:19:08 26 4
gpt4 key购买 nike

我最近在面试时被问到这个问题,但我无法解决。我想把它贴在这里,看看是否有人能给我一些关于如何解决此类问题的想法。

问题:给定正 X 轴和 Y 轴...

  1. 首先画一个宽度为 A 的正方形(比如 A=1000)
  2. 接下来在正方形内画一个最大可能半径的圆,使圆接触正方形的所有 4 条边。
  3. 接下来在这个圆内画一个正方形,使正方形的所有 4 个顶点都与圆接触。
  4. 不断重复上述过程,不断交替绘制圆形和正方形,直到刚刚绘制的形状的面积为< B(假设B=10)。

编写伪代码/逻辑来实现这一点。

这是一个示例图,显示了面试官的问题(请原谅畸形部分)。

Sample diagram, obviously, the question is theoretical, so don't read too much into the exact shape of my drawing

最佳答案

您有 2 个案例:

  1. 给定一个正方形的宽度,那么圆的面积就是A = (宽度^2)*pi/4

  2. 给定一个圆的半径,那么正方形的面积就是A = 2*(R^2)

在 python 中:

R=0
width=1000
result=1000000
B=10
square=True

while result > B:
if square:
R=width/2
result=math.pi*(R*R)
else:
width=R*math.sqrt(2)
result=2*R*R
square = not square

print(result)

关于algorithm - 采访 : draw shapes (circles & squares) until area is less than X,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38407803/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com