我如何继续,我哪里弄错了?

2024-09-28 03:21:27 发布

您现在位置:Python中文网/ 问答频道 /正文

问题是:

Give numBottles(input) full water bottles, you can exchange numExchange(input) empty bottles for one full water bottle. The operation of drinking a full water bottle turns it into an empty bottle. Return the maximum number of water bottles you can drink. No functions, imports, whatsoever, only list, string, int, loops and that kinda stuff.

Ex.1:

Input(s): numBottles = 9, numExchange = 3 Output: 13 Explanation: You can exchange 3 empty bottles to get 1 full water bottle. Number of water bottles you can drink: 9+3+1 = 13

Ex.2: Input(s) :numbottles = 15, numexchange = 4 Output: 19 Explanation: You can exchange 4 empty bottles to get 1 full water bottle. Number of water bottles you can drink: 15 + 3 + 1 = 19

我得到的是:

    Numbottles = int(input("Numbottles"))
    Numexchange = int(input("NumExchange"))
    total = []
    for i in range(0,Numbottles // Numexchange):
            x = Numbottles % Numexchange
            total.append(x)
    print(sum(total))

Tags: ofyoubottleinputexchangecanfullempty
1条回答
网友
1楼 · 发布于 2024-09-28 03:21:27

请试试这个

Numbottles = int(input("Numbottles"))
Numexchange = int(input("NumExchange"))
total = [Numbottles]
for i in range(0,Numbottles // Numexchange):
        Numbottles = Numbottles // Numexchange
        total.append(Numbottles)
        Numbottles += Numbottles % Numexchange
print(sum(total))

相关问题 更多 >

    热门问题