有一天你贡献了一个非常好的idea,老板给了你一笔奖金。为了庆祝,你把你的朋友们带到可怕的潜水酒吧,并使用奖金购买和建造最大的三维啤酒罐金字塔。然后与朋友们进入狂欢。
啤酒罐金字塔建造的规则是:顶层1罐,第二层4罐,下一层9罐,下一层16罐,25罐…
完成 beeramid 函数以返回您可以制作的啤酒罐完整金字塔的层数,给定以下参数:
您的奖金 bonus,以及啤酒的价格 price
例如:
beeramid(1500, 2) # should === 12
beeramid(5000, 3) # should === 16
题目难度:中等
题目来源:https://www.codewars.com/kata/51e04f6b544cf3f6550000c1
def beeramid(bonus: float, price: float) -> int:
# your code here
assert beeramid(9, 2) == 1
assert beeramid(10, 2) == 2
assert beeramid(11, 2) == 2
assert beeramid(21, 1.5) == 3
assert beeramid(454, 5) == 5
assert beeramid(455, 5) == 6
assert beeramid(4, 4) == 1
assert beeramid(3, 4) == 0
assert beeramid(0, 4) == 0
assert beeramid(-1, 4) == 0
assert beeramid(10500, 2) == 24
xiaopeng
(霍格沃兹_小鹏)
2022 年8 月 18 日 03:12
4
def beeramid(bonus: float, price: float) -> int:
num = int(bonus / price)
count = -1
index = 1
while (num >= 0):
count += 1
num -= index ** 2
index += 1
print(count)
return count
1 个赞
def beeramid(bonus, price):
layers,sum = 0,0
while sum <= bonus:
layers += 1
sum += (layers**2) * price
return max(layers-1,0)
1 个赞
手机…
解题思路:
1、根据啤酒摆金字塔罐数的规律,每层的数量,
得出来的数字规律是:
总罐数=11+2 2+3*+44+…+n n
n 就是啤酒能摆出来的层数
2、循环 ii i=1 i++ 汇总叠加i i的值(可摆的总罐数)
总罐数是否小于 用户输入的罐数
得出最后能摆出的层数
def beeramid(bonus: float, price: float) -> int:
# your code here
sum=0
total=int(bonus/price)
list=[]
if total==1:
return 1
for i in range(1,total):
sum += i ** 2
if sum<=total:
list.append(i**2)
return len(list)
def beeramid(bonus: float, price: float):
s = int(bonus / price)
i = 1
floor=0
count = 0
while s > 0:
count = count + i * i
if s >= count:
i = i + 1
floor = floor + 1
continue
else:
break
print(floor)
return int(floor)
def beeramid(bonus: float, price: float) -> int:
if bonus <= 0 or price <= 0:
return 0
num = bonus // price
beer_sum = 0
for i in range(0, int(num) + 1):
beer_sum += i ** 2
if beer_sum > num:
i -= 1
break
return i
assert beeramid(9, 2) == 1
assert beeramid(10, 2) == 2
assert beeramid(11, 2) == 2
assert beeramid(21, 1.5) == 3
assert beeramid(454, 5) == 5
assert beeramid(455, 5) == 6
assert beeramid(4, 4) == 1
assert beeramid(3, 4) == 0
assert beeramid(0, 4) == 0
assert beeramid(-1, 4) == 0
assert beeramid(10500, 2) == 24
def beeramid(bonus: float, price: float) -> int:
layer = 0
cost = 0
if bonus <=0 or price <= 0:
return 0
while cost <= bonus:
layer += 1
cost = cost + layer**2*price
return layer - 1
Lili
(谢丽丽)
2023 年2 月 14 日 05:12
11
def beeramid(bounds, price):
if bounds < 0 or price <= 0:
print("请输入正确的bounds和price")
return
beer_num = bounds//price
floor = 0
for i in range(1, beer_num+1):
num = i**2
if beer_num - num >= 0:
floor += 1
beer_num -= num
else:
break
return floor
lekaixin
(ALe阿乐)
2023 年12 月 6 日 05:56
12
def beeramid(bonus: float, price: float) -> int:
if bonus<0:
return 0
else:
num=bonus//price
floor=0
res_num=0
while res_num<=num:
floor += 1
res_num+=floor**2
return floor-1
assert beeramid(9, 2) == 1
assert beeramid(10, 2) == 2
assert beeramid(11, 2) == 2
assert beeramid(21, 1.5) == 3
assert beeramid(454, 5) == 5
assert beeramid(455, 5) == 6
assert beeramid(4, 4) == 1
assert beeramid(3, 4) == 0
assert beeramid(0, 4) == 0
assert beeramid(-1, 4) == 0
assert beeramid(10500, 2) == 24