公司官网网站建设想法,上海app开发定制公司,源码下载器,正规网站建设官网CheckiO 是面向初学者和高级程序员的编码游戏#xff0c;使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务#xff0c;从而提高你的编码技能#xff0c;本博客主要记录自己用 Python 在闯关时的做题思路和实现代码#xff0c;同时也学习学习其他大神写的代码。
Chec…
CheckiO 是面向初学者和高级程序员的编码游戏使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务从而提高你的编码技能本博客主要记录自己用 Python 在闯关时的做题思路和实现代码同时也学习学习其他大神写的代码。
CheckiO 官网https://checkio.org/
我的 CheckiO 主页https://py.checkio.org/user/TRHX/
CheckiO 题解系列专栏https://itrhx.blog.csdn.net/category_9536424.html
CheckiO 所有题解源代码https://github.com/TRHX/Python-CheckiO-Exercise 题目描述
【Feed Pigeons】此题任务是模拟喂鸽子最开始只有 1 只鸽子1 分钟后飞来 2 只鸽子再过 1 分钟飞来了 3 只以此类推123 4…1 份饲料可以让鸽子吃 1 分钟如果没有足够的食物会让先到的鸟先吃鸽子永远不会停止进食如果我有 N 份饲料有多少鸽子至少吃到一份饲料
举例我有 5 份饲料
第 1 分钟只有 1 只鸽子 A先给 A 喂 1 份还剩下 4 份饲料 第 2 分钟原来有鸽子 A又飞来两只鸽子 B 和 C先给 A 喂 1 份再给 B 和 C 各喂 1 份还剩下 1 份饲料 第 3 分钟原来有鸽子 A、B、C又飞来 3 只鸽子 D、E、F给 A 喂 1 份饲料喂完
此时A 吃了 3 次饲料、B 和 C 吃了 1 次饲料D、E、F 没有吃到饲料所以返回结果为 3。
【链接】https://py.checkio.org/mission/feed-pigeons/
【输入】饲料的份数int
【输出】已经喂食过的鸽子的数目int
【前提】0 N 105
【范例】
checkio(1) 1
checkio(2) 1
checkio(5) 3
checkio(10) 6代码实现
def checkio(number):fed minute pigeons 0while number 0:number - pigeonsminute 1if number 0:return fedif minute number:fed minutenumber - minuteelse:fed numberreturn fedpigeons minutereturn fedif __name__ __main__:# These asserts using only for self-checking and not necessary for auto-testingassert checkio(1) 1, 1st exampleassert checkio(2) 1, 2nd exampleassert checkio(5) 3, 3rd exampleassert checkio(10) 6, 4th example大神解答 大神解答 NO.1 Determine the number of (greedy) pigeons who will be fed.
import itertoolsdef checkio(food):Given a quantity of food, return the number of pigeons who will eat.pigeons 0for t in itertools.count(1):if pigeons t food:# The food will be consumed this time step.# All pigeons around last time were fed, and there is enough food# this time step to feed food pigeons, so return the max of each.return max(pigeons, food)# Increase pigeons, decrease food.pigeons tfood - pigeons大神解答 NO.2 def checkio(number):sum 0m 1n 0while(sum number):n m*(m1)/2sum sum nm m 1if (sum - number) m-1:return (m-21)*(m-2)/2else:return n - (sum - number)大神解答 NO.3 def checkio(food):birds new 0while food 0:new 1birds newfood - birdsreturn birds max(food, -new)大神解答 NO.4 def allpigeon(min):if min0:return 0return allpigeon(min-1)mindef allneed(min):if min0:return 0 return allneed(min-1)allpigeon(min) def checkio(number):i0while allneed(i)number:i1curnumber-allneed(i-1)if curallpigeon(i-1):return curreturn allpigeon(i-1)大神解答 NO.5 def checkio(n): # explanation follows...p lambda t: t * (t1) // 2q lambda t: (t*t*t 3*t*t 2*t) // 6h 9*n*n - 1/27R 3*n h**(1/2)T 3*n - h**(1/2)X1 R**(1/3) T**(1/3) - 1w int(X1)return p(w) max(0, n-q(w)-p(w))p(t): number of of pigeons at round tp(1) 1p(n) p(n-1) np(n) 1 2 3 ... n n*(n1)/2q(t): number of portions to feed all pigeons in the first t roundsq(t) \sum_{i1}^{n} p(i) 1/2 * \sum_{i1}^{n} n^2 1/2 * \sum_{i1}^{n} n 1/2 * n * (n1) * (2*n1) / 6 1/2 * n * (n1) / 2 1/12 * (2*n^3 3*n^2 n) 1/4 * (n^2 n) 1/12 * (2*n^3 3*n^2 n 3*n^2 3*n) 1/12 * (2*n^3 6*n^2 4*n) 1/6 * (n^3 3*n^2 2*n)Suppose we start with N portions and w full rounds of pidgeons are fed:q(w) Nw^3 3*w^2 2*w - 6*N 0Single real root is calculated by:a 1, b 3, c 2, d -6*Nf (3*c/a - b*b/a/a)/3g (2*b*b*b/a/a/a - 9*b*c/a/a 27*d/a)/27h g*g/4 f*f*f/27R -(g/2) h**(1/2)S R**(1/3)T -(g/2) - h**(1/2)U T**(1/3)X1 S U - b/3/atheferore: w int(X1)We can feed p(w) pidgeons and we are left with N - q(w) portions for round w1.
But the first p(w) pidgeons in round w1 have already been fed.
So, if N - q(w) p(w), we can feed N - q(w) - p(w) more pidgeons.