[英]I am facing problem with logic of my code
我是編碼新手,正在嘗試通過 codechef 解決問題
問題代碼 = AUCTION
愛麗絲、鮑勃和查理正在拍賣會上競標一件神器。 Alice 出價 AA 盧比,Bob 出價 BB 盧比,Charlie 出價 CC 盧比(其中 AA、BB 和 CC 是不同的)。 根據拍賣規則,出價最高的人將贏得拍賣。 確定誰將贏得拍賣。
我用python寫了代碼
t = int(input())
for i in range(t):
n = (a,b,c) = map(int,input().split())
a , b , c = "Alice","Bobs","Charlie"
highest = max(n, default = 0)
highest = a or b or c
print(highest)
我的輸入
200 100 400
155 1000 566
736 234 470
124 67 2
我的輸出
Alice
Alice
Alice
Alice
不確定您發明什么算法來解決該任務,但顯然問題出在此處:
highest = max(n, default = 0)
highest = a or b or c
您根本不使用第一個highest
值(我猜這不是您想要的),第二個最高值是在構造a or b or c
上計算,它返回的第一個值評估為True
。
print(bool("Alice")) # prints True
這就是為什么highest
總是等於"Alice"
作為你,為了解決我會使用的任務, if
a, b, c = map(int, input().split())
if a > b:
if a > c:
print("Alice")
else:
print("Charlie")
else:
if b > c:
print("Bob")
else:
print("Charlie")
因此,您在此處將輸入映射到 a、b、c:
n = (a,b,c) = map(int,input().split())
但是你也給 a, b, c 不同的字符串值: a , b , c = "Alice","Bobs","Charlie"
你應該做的是檢查輸入中的哪個值更大(a或b或c)相應地打印名稱,如下所示:
if max(n)== a :
print("Alice")
if max(n)== b :
print("Bob")
if max(n)== c :
print("Charlie")
我認為你有點把問題復雜化了。
對那些簡單。
alice = int(input()), "Alice"
bob = int(input()), "Bob"
charlie = int(input()), "Charlie"
print(max(alice, bob, charlie)[1],"has the highest bet.")
此外,您不應該重新分配變量,除非它是您在更復雜算法中的重點。
也看看真值表:可以幫助你解釋條件語句。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.