【Python CheckiO 题解】Feed Pigeons
CheckiO 是面向初學(xué)者和高級(jí)程序員的編碼游戲,使用 Python 和 JavaScript 解決棘手的挑戰(zhàn)和有趣的任務(wù),從而提高你的編碼技能,本博客主要記錄自己用 Python 在闖關(guān)時(shí)的做題思路和實(shí)現(xiàn)代碼,同時(shí)也學(xué)習(xí)學(xué)習(xí)其他大神寫的代碼。
CheckiO 官網(wǎng):https://checkio.org/
我的 CheckiO 主頁(yè):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】:此題任務(wù)是模擬喂鴿子,最開始只有 1 只鴿子,1 分鐘后飛來 2 只鴿子,再過 1 分鐘飛來了 3 只,以此類推(1+2+3 +4+…),1 份飼料可以讓鴿子吃 1 分鐘,如果沒有足夠的食物,會(huì)讓先到的鳥先吃,鴿子永遠(yuǎn)不會(huì)停止進(jìn)食,如果我有 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 份,飼料喂完;
此時(shí):A 吃了 3 次飼料、B 和 C 吃了 1 次飼料,D、E、F 沒有吃到飼料,所以返回結(jié)果為 3。
【鏈接】:https://py.checkio.org/mission/feed-pigeons/
【輸入】:飼料的份數(shù)(int)
【輸出】:已經(jīng)喂食過的鴿子的數(shù)目(int)
【前提】:0 < N < 105
【范例】:
checkio(1) == 1 checkio(2) == 1 checkio(5) == 3 checkio(10) == 6代碼實(shí)現(xiàn)
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 example"assert checkio(2) == 1, "2nd example"assert checkio(5) == 3, "3rd example"assert 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*(m+1)/2sum = sum + nm = m + 1if (sum - number) >= m-1:return (m-2+1)*(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 min==0:return 0return allpigeon(min-1)+mindef allneed(min):if min==0:return 0 return allneed(min-1)+allpigeon(min) def checkio(number):i=0while allneed(i)<=number:i+=1cur=number-allneed(i-1)if cur>allpigeon(i-1):return curreturn allpigeon(i-1)大神解答 NO.5
def checkio(n): # explanation follows...p = lambda t: t * (t+1) // 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*(n+1)/2q(t): number of portions to feed all pigeons in the first t roundsq(t)= \sum_{i=1}^{n} p(i)= 1/2 * \sum_{i=1}^{n} n^2 + 1/2 * \sum_{i=1}^{n} n= 1/2 * n * (n+1) * (2*n+1) / 6 + 1/2 * n * (n+1) / 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) <= N <=> w^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 w+1. But the first p(w) pidgeons in round w+1 have already been fed. So, if N - q(w) > p(w), we can feed N - q(w) - p(w) more pidgeons. """總結(jié)
以上是生活随笔為你收集整理的【Python CheckiO 题解】Feed Pigeons的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2017年光大银行微信申请信用卡秒批方法
- 下一篇: Python3 基础学习笔记 C06【用