本文介绍了代码有什么问题?结果在Google Kickstart 2020 Round A中产生RE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这段代码中缺少什么?它会在Kickstart 2020中产生 RE ,但是当我在本地计算机或hackereath编译器(类似于codejam编译器)上进行测试时,代码可以正常工作.
问题链接: https://codingcompetitions.withgoogle.com/kickstart/round/000000000019ffc7/00000000001d3f56

What i am missing here in this code? It results in RE in Kickstart 2020 but when i test on my local machine or hackereath compiler(similar to codejam complier) code works fine.
problem link:https://codingcompetitions.withgoogle.com/kickstart/round/000000000019ffc7/00000000001d3f56

def solve(a,balance):
    a.sort()
    house_Can_buy=0
    for i in a:
        if i<=balance:
            house_Can_buy+=1
            balance-=i
    return house_Can_buy
def main():
    a=int(input())
    arr2,res=[],[]
    for i in range(a):
        _,balance=list(map(int,input().split()))
        arr2=list(map(int,input().split()))
        result=solve(arr2,balance)
        res.append(result)
    for i,j in enumerate (res):
        print(f'Case #{i+1}: {j}')
main()

推荐答案

在使用f导致RE的print语句中.

At the print statement in which use of f is resulting in RE.

在打印语句中,应使用format输出i + 1和j.

In the print statement, you should use format to output i+1 and j.

要通过所有测试用例,您必须对数组进行排序以通过贪婪方法计算房屋数量.

To pass all the test cases you have to sort the array to calculate number of houses by the greedy method.

def solve(a,balance):
    house_Can_buy=0
    a.sort()
    for i in a:
        if i<=balance:
            house_Can_buy+=1
            balance-=i
    return house_Can_buy

def main():
    a=int(input())
    arr2,res=[],[]
    for i in range(a):
        _,balance=list(map(int,input().split()))
        arr2=list(map(int,input().split()))
        result=solve(arr2,balance)
        res.append(result)
    for i,j in enumerate (res):
        print('Case #{}: {}'.format(i+1,j))
main()

这篇关于代码有什么问题?结果在Google Kickstart 2020 Round A中产生RE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 05:34