Python 我的codejam练习的解决方案或输出方法有什么不正确之处?
我参加了谷歌代码堵塞竞赛的kickstart,陷入了名为Gbus计数的问题1A 我的代码通过了问题中提供的示例测试用例,但是当我通过将小输入输出到文件来提交时,它会给我一个错误的响应 问题链接-Python 我的codejam练习的解决方案或输出方法有什么不正确之处?,python,python-3.x,algorithm,output,logic,Python,Python 3.x,Algorithm,Output,Logic,我参加了谷歌代码堵塞竞赛的kickstart,陷入了名为Gbus计数的问题1A 我的代码通过了问题中提供的示例测试用例,但是当我通过将小输入输出到文件来提交时,它会给我一个错误的响应 问题链接- t=int(input())#测试用例数 对于范围(t)内的试验: 如果测试==0: n=int(输入()) 其他: 在每个测试用例之后覆盖空白行 n=int(输入()) l=列表(映射(int,input().split()) 公共汽车={}#每辆公共汽车的路线将存储在此词典中 i=1 对于l中的d:
t=int(input())#测试用例数
对于范围(t)内的试验:
如果测试==0:
n=int(输入())
其他:
在每个测试用例之后覆盖空白行
n=int(输入())
l=列表(映射(int,input().split())
公共汽车={}#每辆公共汽车的路线将存储在此词典中
i=1
对于l中的d:#此循环用于将每条总线的路线存储在总线字典中
如果我不在公交车上:
巴士[i]=[d]
elif len(bus[i])我在代码中看到两个问题
Python标准字典未订购!因此,cities
字典将不会按照您输入的顺序包含元素(键)。
- 从Python 3.1开始,您可以使用
如果同一个城市重复两次(或更多),最终结果中将只有一个条目,因为字典不能保存重复的键
正如我所见,您的设计具有流程。此外,我认为你可以很容易地做到这一点,而不会把事情弄得太复杂
注意:
从Python3.6开始,默认情况下,标准字典保持插入顺序
更新
根据要求,以下是我解决问题的尝试
请注意,这可能不是最好的解决方案!当然,这不是最具可读性的解决方案。这就是我的解决方案
for case in range(1, int(input())+1):
if case > 1:
input('\n') # consume and print newline
print("Case #{}:".format(case), end='')
bus_count = int(input())
busline = list(map(int, input().strip().split(" ")))
for _ in range(int(input())):
city = int(input())
matches = sum([1 for bid in range(bus_count) if busline[2*bid] <= city <= busline[2*bid+1]])
print(" {}".format(matches), end='')
适用于范围(1,int(input())+1)中的大小写:
如果案例>1:
输入('\n')#使用并打印换行符
打印(“Case{}:”.format(Case),end='')
总线计数=int(输入())
总线=列表(映射(int,input().strip().split(“”))
对于范围内的(int(input()):
city=int(输入())
matches=sum([1表示范围内的出价(总线计数))如果总线[2*bid]您也可以检查此解决方案:
def Gbus_Count():
T = int(input())
for i in range(T):
if i == 0:
N = int(input())
else:
input()
N = int(input())
A = list(map(int, input().split()))
P = int(input())
C = list()
D = dict()
for k in range(P):
C.append(int(input()))
for m in range(P):
for n in range(N):
if A[2*n]<=C[m]<=A[2*n+1]:
if C[m] not in D:
D[C[m]] = 1
else:
D[C[m]] += 1
print("Case #{}: {}".format(i+1, ' '.join(str(x) for x in D.values())))
Gbus_Count()
def Gbus_Count():
T=int(输入())
对于范围(T)内的i:
如果i==0:
N=int(输入())
其他:
输入()
N=int(输入())
A=列表(映射(int,input().split())
P=int(输入())
C=列表()
D=dict()
对于范围(P)内的k:
C.append(int(input()))
对于范围内的m(P):
对于范围内的n(n):
如果[2*n]在pc上安装空闲python,您只需按f5即可运行程序。如果您的程序不正确,则会显示错误和错误发生的行。您所需要的是您应该知道错误检测。由于这是一个实践练习,如果您感兴趣,我可以在下面的答案中添加我的解决方案。(我最初没有添加它,因为它会破坏你的想法)@Anubis是的,我当然想看看解决这个问题的更好的方法谢谢你指出了错误,我还有一个疑问我的输出方法正确吗?请准确一点,你的意思是什么?python gbus_count.pyoutput.out
这个命令输出代码正确吗是的,正确。请参阅欲知详情
for case in range(1, int(input())+1):
if case > 1:
input('\n') # consume and print newline
print("Case #{}:".format(case), end='')
bus_count = int(input())
busline = list(map(int, input().strip().split(" ")))
for _ in range(int(input())):
city = int(input())
matches = sum([1 for bid in range(bus_count) if busline[2*bid] <= city <= busline[2*bid+1]])
print(" {}".format(matches), end='')
def Gbus_Count():
T = int(input())
for i in range(T):
if i == 0:
N = int(input())
else:
input()
N = int(input())
A = list(map(int, input().split()))
P = int(input())
C = list()
D = dict()
for k in range(P):
C.append(int(input()))
for m in range(P):
for n in range(N):
if A[2*n]<=C[m]<=A[2*n+1]:
if C[m] not in D:
D[C[m]] = 1
else:
D[C[m]] += 1
print("Case #{}: {}".format(i+1, ' '.join(str(x) for x in D.values())))
Gbus_Count()