[英]How to write the coding problem with python?
Three empty cans can be exchanged for a new one.三个空罐可以换一个新的。 Suppose you have N cans of soda, try to use the program to solve how many cans of soda you can drink in the end?
假设你有N罐汽水,试着用程序求解你到底能喝多少罐汽水?
Input description: Input a positive integer N. ex.5 / ex.100输入说明:输入正数 integer N. ex.5 / ex.100
Output description: The maximum number of sodas that can be drunk, and must have a newline character at the end. Output 说明:最多可以喝多少苏打水,最后必须有一个换行符。 ex.7 / ex.149 `
ex.7 / ex.149`
n = int(input())
a = n-3
sum = 0
while a > 2 :
sum += 1
a -= 3
print(f'{n+sum}')
if a == 2 :
print(f'{n+sum+1}')
` `
I used while to finish the code which is on above, but I input 5 and output 6,and it is actually to be 7.The other side, I input 100 and output 132. Actually, the correct answer is 149.我用while完成了上面的代码,但是我输入了5和output 6,实际上是7。另一边,我输入了100和output 132。实际上,正确答案是149。
You can try this way -你可以这样试试——
def get_total_cans(n):
s = n # we can get at least n cans
while n > 2:
s += 1
n -= 3 # 3 exchanged
n += 1 # got 1 for the exchange
return s
n = int(input())
print(get_total_cans(n))
The logic is simple and comments are added to explain.逻辑简单,加注释说明。
In your code, the first thing to notice it generates wrong output when n is less than 3. For example for n = 2, your output is 3 which is not possible.在您的代码中,首先要注意的是,当 n 小于 3 时,它会生成错误的 output。例如,对于 n = 2,您的 output 是 3,这是不可能的。 Also in the while loop you are decrementing
a
by 3 for the exchange but you fail to add 1 to a
for the one soda you can exchanging 3 empty cans.同样在 while 循环中,您将
a
递减 3 以进行交换,但您无法将 1 添加到a
中,因为您可以交换 3 个空罐的苏打水。 That is the issue.这就是问题所在。 My above code addresses those issues
我上面的代码解决了这些问题
Here's a recursive approach:这是一种递归方法:
def dr_cans(full, empty=0):
# if we have at least 3 empty cans, exchange them for a full one
if empty >=3:
return dr_cans(full+1,empty-3)
# no full cans, and not enough empty ones
if full == 0:
return 0
# at least one full can: drink it and gain an empty one
return 1 + dr_cans(full-1, empty+1)
If I understand corretly the question is as follows: Let k
be the index of the exchange process.如果我理解正确,问题如下:设
k
为交换过程的索引。 Since not all N
can be divided by 3 we have N[k] = floor(M/3)
, where M=N[k-1]+R[k-1]
new cans in each step.由于并非所有
N
都可以除以 3,因此我们有N[k] = floor(M/3)
,其中M=N[k-1]+R[k-1]
新罐头在每个步骤中。 Plus some R[k] = M%3
leftover cans, where %
is the modulo operator...加上一些
R[k] = M%3
剩余的罐头,其中%
是模运算符...
With this is should be quite easy...有了这个应该很容易......
def compute_num_cans(empty_cans: int, exchange: int = 3) -> tuple:
"""
:param empty_cans: The number of cans to exchange
:return: tuple of (full_cans, empty_cans), where the empty cans are < exchange rate
"""
leftovers = empty_cans % exchange
full = empty_cans // exchange
return full, leftovers
EXCHANGE = 3
NUM_CANS = 51
print(f'Start with {NUM_CANS} and an exchange rate of {EXCHANGE}:1')
current_cans = NUM_CANS
drunk_cans = NUM_CANS
leftovers = 0
steps = 0
while current_cans >= EXCHANGE:
full, leftovers = compute_num_cans(current_cans, exchange=EXCHANGE)
current_cans = full + leftovers
drunk_cans += full
steps += 1
print(f'Cans drunk: {drunk_cans}, leftover cans: {leftovers}.')
print(f'A total of {steps} exchanges was needed.')
This yields as output这产生 output
# Start with 51 and an exchange rate of 3:1
# Cans drunk: 76, leftover cans: 0.
# A total of 4 exchanges was needed.
General Answer:- Accepted also if we change the numExchange also.一般答案:-如果我们也更改 numExchange,也会接受。
Code:-代码:-
def numWaterBottles(numBottles: int, numExchange: int) -> int:
ans=numBottles #Initial bottles he/she will drink
while numBottles>=numExchange: #If numBottles<numExchange exit the while loop
remainder=numBottles%numExchange #remaining bottles which is not change
numBottles//=numExchange #The bottles which are changed
ans+=numBottles #The bottles which are changed added to the answer
numBottles+=remainder #Remaining bottles==The bottles which is not change+The bottles which are changed
return ans #Return The answer
print(numWaterBottles(5,3))
print(numWaterBottles(100,3))
print(numWaterBottles(32,4)) #numexchange when different
Output:- Output:-
7
149
42
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.