繁体   English   中英

我的代码逻辑遇到问题

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM