Nicola needs some help building a circular landing zone using the ice square tiles for their new Ice Base. Before he converts the area to a construction place, Nikola needs to figure out how many square tiles he will need.
Each square tile has size of 1x1 meters. You need to calculate how many whole and partial tiles are needed for a circle with a radius of N meters. The center of the circle will be at the intersection of four tiles. For example: a circle with a radius of 2 metres requires 4 whole tiles and 12 partial tiles.

Input: The radius of a circle as a float
Output: The quantities whole and partial tiles as a list, [solid, partial] as numbers.
|
1
2
3
4
|
checkio(2)==[4,12]
checkio(3)==[16,20]
checkio(2.1)==[4,20]
checkio(2.5)==[12,20]
|
from math import sqrt,ceil
def checkio(radius):
tiles = partial = 0
left_height = radius
length = int(ceil(radius))
square_radius = radius**2
for x in range(1, length):
height = sqrt(square_radius - x**2)
tiles += int(height)
partial += int(ceil(left_height)) - int(height)
left_height = height
partial += int(ceil(left_height))
return [tiles*4, partial*4]
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio(2) == [4, 12], "N=2"
assert checkio(3) == [16, 20], "N=3"
assert checkio(2.1) == [4, 20], "N=2.1"
assert checkio(2.5) == [12, 20], "N=2.5"
本博客介绍如何计算构建圆形冰基地时所需的冰块总数,包括整块和部分块的数量,通过优化算法减少计算复杂度。
786

被折叠的 条评论
为什么被折叠?



