可爱的幸运羊羔最后一个测试用例未通过 [英] lovely-lucky-lambs last test case not passing

查看:197
本文介绍了可爱的幸运羊羔最后一个测试用例未通过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我参加Google foobar派对很晚.我被困在2级,只有最后一个测试用例处于待处理状态.但是我完全不知道这个问题在最后一个测试用例中会发生什么.

I am being very late to the google foobar party. I am stuck at level 2, and only the last test case is pending. But I am completely clueless of what this question is expecting in this last test case.

我在问题上进行了搜索,看起来他们已经更新了测试用例和羔羊约束< 10已被删除.

I googled the question and looks like they've updated the test case and the constraint of lambs < 10 has been removed.

问题是:

可爱的幸运羔羊

身为黑种人并不单单是徒劳.有时候,当司令 Lambda感觉很慷慨,她会派发幸运的LAMB(Lambda的 万能钱块).武装分子可以使用幸运的羔羊来买东西 像是第二双袜子,铺床上的枕头,甚至是第三双 每日一餐!

Lovely Lucky LAMBs

Being a henchman isn't all drudgery. Occasionally, when Commander Lambda is feeling generous, she'll hand out Lucky LAMBs (Lambda's All-purpose Money Bucks). Henchmen can use Lucky LAMBs to buy things like a second pair of socks, a pillow for their bunks, or even a third daily meal!

但是,要使LAMB真正传出去并不容易.每个小伙子 具有严格的资历等级,必须予以尊重-否则, 武装分子将起义,你们都将再次被降级为奴才!

However, actually passing out LAMBs isn't easy. Each henchman squad has a strict seniority ranking which must be respected - or else the henchmen will revolt and you'll all get demoted back to minions again!

您必须遵循4条关键规则,以避免 反叛: 1.最低级的小伙子(资历最少)刚好得到1个LAMB. (一个团队中总是至少有1个随从.) 2.如果名列前茅的人获得的LAMB数量增加一倍以上,则将引发叛乱. 3.如果向其下两个下属提供的LAMB数量超过他们的LAMB数量,则该小伙子将起义 得到. (请注意,两个最初级的小伙子不会有两个 下属,因此此规则不适用于他们.初中排名第二 nch夫将至少需要与最初级的LAMB一样多的LAMB 小伙子.) 4.您总能找到更多的要支付的指挥官-司令官拥有大量员工.如果剩余足够的LAMB,从而 可以在遵从 其他规则,则您必须始终添加并支付该小伙子的费用.

There are 4 key rules which you must follow in order to avoid a revolt: 1. The most junior henchman (with the least seniority) gets exactly 1 LAMB. (There will always be at least 1 henchman on a team.) 2. A henchman will revolt if the person who ranks immediately above them gets more than double the number of LAMBs they do. 3. A henchman will revolt if the amount of LAMBs given to their next two subordinates combined is more than the number of LAMBs they get. (Note that the two most junior henchmen won't have two subordinates, so this rule doesn't apply to them. The 2nd most junior henchman would require at least as many LAMBs as the most junior henchman.) 4. You can always find more henchmen to pay - the Commander has plenty of employees. If there are enough LAMBs left over such that another henchman could be added as the most senior while obeying the other rules, you must always add and pay that henchman.

请注意,您可能无法分发所有LAMB.一个LAMB 无法细分.也就是说,所有的好心人都必须得到积极的回应. LAMB的整数.

Note that you may not be able to hand out all the LAMBs. A single LAMB cannot be subdivided. That is, all henchmen must get a positive integer number of LAMBs.

编写一个称为solution(total_lambs)的函数,其中total_lambs为 您要除法的讲义中LAMB的整数. 它应该返回一个整数,该整数表示 可以共享LAMB的伙伴的最小和最大数量 (也就是说,对您所付的款项尽可能地宽容和小气 尽可能分别),同时仍然遵守上述所有规则 避免起义.例如,如果您有10个LAMB,并且 尽可能地慷慨,您只能付给3个追随者(1、2和4 LAMB(按升序排列),但如果您一样小气 您可能会支付4名追随者(1、2、3和3辆LAMB). 因此,solution(10)应该返回4-3 = 1.

Write a function called solution(total_lambs), where total_lambs is the integer number of LAMBs in the handout you are trying to divide. It should return an integer which represents the difference between the minimum and maximum number of henchmen who can share the LAMBs (that is, being as generous as possible to those you pay and as stingy as possible, respectively) while still obeying all of the above rules to avoid a revolt. For instance, if you had 10 LAMBs and were as generous as possible, you could only pay 3 henchmen (1, 2, and 4 LAMBs, in order of ascending seniority), whereas if you were as stingy as possible, you could pay 4 henchmen (1, 1, 2, and 3 LAMBs). Therefore, solution(10) should return 4-3 = 1.

为使事情变得有趣,指挥官Lambda更改了 幸运的LAMB支出.您可以期望total_lambs始终是积极的 小于10亿的整数(10 ^ 9).

To keep things interesting, Commander Lambda varies the sizes of the Lucky LAMB payouts. You can expect total_lambs to always be a positive integer less than 1 billion (10 ^ 9).

我的解决方法是:

def generous(i):
    num = 0
    initial=0
    while initial + 2**num <= i:
        initial = initial + 2**num
        num = num + 1
    if i - initial >= 2**(num-1)+2**(num-2):
        num = num + 1
    return num
def stingy(i):
    first = 1
    second = 1
    total = 0
    num = 0
    while total+first<=i:
        total = total + first
        temp = second
        second = temp + first
        first = temp
        num = num + 1
    return num
def solution(total_lambs):
    if total_lambs >= 1000000000:    return 0
    gen = generous(total_lambs)
    sti = stingy(total_lambs)
    return max(sti,gen)- min(gen,sti)

我尚未测试我的代码是否超过了时间限制. TLE的错误消息是否也可能失败?

I haven't tested my code for Time limit exceeds. Is it possible that the error message for TLE is also FAILED ?

推荐答案

我也一直坚持这个问题,我试图解决这个问题并获得成功.我在这里提供我的代码,该代码通过了我自己测试的所有测试用例.我相信这会有所帮助.

I have stuck on this problem too, I tried to solve this problem and got success on this. I am providing my code here which passed all test cases tested by myself. I am sure it will help.

def solution(total_lambs):
if total_lambs >= 10**9:
    return 0
doubledList=[]
x=0
runningtotal=0
while x<= total_lambs:
    currentvalue=2**x
    doubledList.append(currentvalue)
    runningtotal=runningtotal + currentvalue
    if runningtotal > total_lambs:
        break
    x=x+1
fiblist=[1,1]
fibrunningtotal=2
y=2
while y<= total_lambs:
    value=fiblist[y-1] + fiblist[y-2]
    fiblist.append(value)
    fibrunningtotal=fibrunningtotal + int(fiblist[y])
    if fibrunningtotal > total_lambs:
        break
    y=y+1
solution = len(fiblist) - len(doubledList)
return abs(solution)

这篇关于可爱的幸运羊羔最后一个测试用例未通过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆