首页 > 解决方案 > 正确的示例输出但未在 codechef 上被接受

问题描述

我正在尝试使用 Python 语言解决这个初学者问题:https ://www.codechef.com/problems/ZUBTRCNT

我已经编写了在示例案例中给出正确输出的代码。它提供与其他接受的答案相同的输出。但是我的代码没有被接受。

供参考:(我的代码不被接受):https ://www.codechef.com/viewsolution/40506063

case=int(input())
for c in range(case):
    l,k=map(int,input().split())
    n = l - k + 1
    sum = n * (n + 1) // 2
    print("Case {}:".format(c+1),sum)

(另一人成功回答):https ://www.codechef.com/viewsolution/39844942

t=int(input())
for i in range(0,t):
   l,k=map(int,input().split())
   if(l<k):
      print("Case "+str(i+1)+":","0")
   else:
      m=l-k+1
      m=m*(m+1)//2
      print("Case "+str(i+1)+":",m)

标签: pythonpython-3.x

解决方案


这是因为您错过了一个重要的测试用例l<k

看看下面的代码,它在 Codechef 上有一个Accepted的判断:

case=int(input())
for c in range(case):
    l,k=map(int,input().split())
    if(l<k):
      print("Case {}:".format(c+1),0)
    else:
        n = l - k + 1
        sum = n * (n + 1) // 2
        print("Case {}:".format(c+1),sum)

判决:

在此处输入图像描述


推荐阅读