Python 谷歌启动错误答案
我为Google Kickstart 2020 a轮制作了一个程序,但是它显示了错误的答案,并且跳过了测试集。 这就是问题所在 问题 有N栋房子待售。买第i栋房子要花Ai美元。你的预算是B美元 你最多能买多少套房子? 输入 输入的第一行给出了测试用例的数量,然后是T.T测试用例。每个测试用例从一行开始,其中包含两个整数N和B。第二行包含N个整数。第i个整数是Ai,第i个房子的成本。 输出 对于每个测试用例,输出一行包含用例#x:y,其中x是测试用例编号(从1开始),y是您可以购买的最大房屋数量。 极限 时间限制:每个测试集15秒。 内存限制:1GB。 1.≤ T≤ 100 1.≤ B≤ 105 1.≤ 艾岛≤ 1000美元,我所有的钱。 测试集1 一,≤ N≤ 100 测试集2 一,≤ N≤ 105 样品 输入 三, 4 100 20 90 40 90 4 50 30 30 10 10 3 300 999 999 999 输出 案例#1:2 案例2:3 案例#3:0 在示例案例1中,您的预算为100美元。你可以花20+40=60美元购买第一套和第三套房子。 在示例案例2中,您的预算为50美元。你可以花30+10+10=50美元购买第一、第三和第四套房子。 在示例案例3中,您的预算为300美元。你不能买任何房子(所以答案是0) 注意:与以前的版本不同,在Kick Start 2020中,所有测试集都是可见的测试集,这意味着您在提交时会收到即时反馈 我的密码是Python 谷歌启动错误答案,python,Python,我为Google Kickstart 2020 a轮制作了一个程序,但是它显示了错误的答案,并且跳过了测试集。 这就是问题所在 问题 有N栋房子待售。买第i栋房子要花Ai美元。你的预算是B美元 你最多能买多少套房子? 输入 输入的第一行给出了测试用例的数量,然后是T.T测试用例。每个测试用例从一行开始,其中包含两个整数N和B。第二行包含N个整数。第i个整数是Ai,第i个房子的成本。 输出 对于每个测试用例,输出一行包含用例#x:y,其中x是测试用例编号(从1开始),y是您可以购买的最大房屋数量。
T = int(input())
caselist = []
for i in range(T):
x = input()
x = x.split()
N = int(x[0])
B = int(x[1])
l = input()
l = l.split()
temp = []
for j in l:
temp.append(int(j))
temp.sort()
s = 0
n = 0
for k in temp:
s+=k
if s<=B:
n+=1
else:
print('Case #'+str(i+1)+': '+str(n))
break
T=int(输入())
案例列表=[]
对于范围(T)内的i:
x=输入()
x=x.split()
N=int(x[0])
B=int(x[1])
l=输入()
l=l.split()
温度=[]
对于l中的j:
临时附加(int(j))
临时排序()
s=0
n=0
对于k in温度:
s+=k
如果你的代码的问题是你是否可以买所有的房子,你不打印答案,因为你只打印一次总成本超过你的预算。这可以通过在程序结束时将打印语句移出循环来轻松解决。\include
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
int n, b, a[100000];
void solve()
{
cin >> n >> b;
for(int i=0; i<n; ++i)
cin >> a[i];
sort(a, a+n);
int ans=0;
for(int i=0; i<n; ++i) {
if(b>=a[I])
{
b-=a[i];
++ans;
}
}
cout << ans << "\n";
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t, i=1;
cin >> t;
while(t--) {
cout << "Case #" << i << ": ";
solve();
++i;
}
}
使用名称空间std;
#定义ll long long
#定义ar阵列
整数n,b,a[100000];
void solve()
{
cin>>n>>b;
对于(int i=0;i>a[i];
排序(a,a+n);
int ans=0;
for(int i=0;i=a[i])
{
b-=a[i];
++ans;
}
}
库特;
而(t--){
cout使用JAVA启动2020年A轮(分配问题解决方案)
import java.util.Collections;
import java.util.Scanner;
class Solution {
private static long N;
private static long A;
private static long B;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
for (long i = 0; i < N; i++) {
A = sc.nextInt();
B = sc.nextInt();
ArrayList<Integer> houses = new ArrayList<Integer>();
for (long j = 0; j < A; j++) {
houses.add(sc.nextInt());
}
long no_of_houses = findTheHouses(houses,B);
System.out.println("Case #"+ (i+1) +": "+no_of_houses);
}
sc.close();
}
private static long findTheHouses(ArrayList<Integer> houses, long max_cost) {
Collections.sort(houses);
long count_budget =0;
int houses_count=0;
do{
count_budget = count_budget +houses.get(houses_count);
if(count_budget<=max_cost)
houses_count++;
else
break;
}while(houses.size()>houses_count);
return houses_count;
}
}
import java.util.Collections;
导入java.util.Scanner;
类解决方案{
私有静态长N;
私人静长;
私有静态长B;
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
N=sc.nextInt();
用于(长i=0;i
tt=int(输入())
对于范围内的测试用例(tt):
编号,预算=列表(映射(int,input().split())
打印(数字、预算)
price=[]
购物车=[]
计数=0
对于范围内的h(数量):
price.append(int(input()))
价格为x:
如果xi=int(输入())
num=[]
对于范围(i)中的j:
l=输入()
x,y=l.拆分()
y=int(y)
m=输入()
p=[]
p=m.split(“”)
总和=0
计数=0
s=[]
对于p中的w:
w=int(w)
s、 附加(w)
s=已排序(s)
对于s中的q:
q=int(q)
如果sum+qT=int(输入())
对于范围(T)内的试验:
众议院=[]
input1=输入()
input1=input1.split()
N=int(输入1[0])
B=int(输入1[1])
input2=输入()
input2=input2.split()
对于输入2中的x:
价格=整数(x)
字典={house:x,“price:price}
house.append(字典)
def myFunc(e):
返回e[“价格”]
house.sort(key=myFunc)
花费=0
购买=0
对于x内部:
variable1=x.get('price')
花费+=变量1
如果这是刚刚问到的。你能带我们看看你的算法吗?什么测试失败了?他们没有显示任何其他细节你可以监控该线程以获得你的问题的答案,但算法看起来是相同的。希望他们会编辑它以添加细节。我先在外部使用它。但它仍然有相同的问题,所以我把它放在内部还是那样的…我只是试着在循环结束后用print语句提交它,它通过了测试用例。你记得把break语句保存在循环中吗?如果你也能解释一下你的算法,那就太好了。你能提供一些解释,让其他人投票支持你吗?Himanshu,欢迎来到Stackoverflow。While您的贡献是非常受欢迎的,很难理解您的代码在这个问题上实际做了什么。请解释它的各个部分,以及它们是如何解决原始问题的。嗨,Mukund,欢迎这么做。人们通常不喜欢没有解释的答案。请在an中添加一些解释回答,谢谢
price = []
cart = []
count = 0
for h in range(number):
price.append(int(input()))
for x in price:
if x <= budget:
cart.append(x)
cart.sort()
our_sum = sum(cart)
for y in cart:
count+=1
if our_sum <= budget:
print("Case #",test_cases,": ",count)
tests = int(input())
out = []
for i in range(tests):
inp = input().split(" ")
num = int(inp[0])
b = int(inp[1])
prices = input().split(" ")
for i in range(num):
prices[i] = int(prices[i])
prices.sort()
count = 0
for i in range(num):
if(b-prices[i] >= 0 ):
b = b - prices[i]
count = count+ 1
out.append(count)
for i in out:
print(i)
i = int(input())
num = []
for j in range(i):
l = input()
x , y = l.split()
y = int(y)
m = input()
p =[]
p = m.split(" ")
sum = 0
count = 0
s = []
for w in p:
w = int(w)
s.append(w)
s = sorted(s)
for q in s:
q = int(q)
if sum+q <= y:
sum += q
count+=1
r = count
num.append(r)
i = 0
for op in num:
i +=1
j = "Case #"+str(i)+": "
print(j,end="")
print(op)
T = int(input())
for test in range(T):
house = []
input1 = input()
input1 = input1.split()
N = int(input1[0])
B = int(input1[1])
input2 = input()
input2 = input2.split()
for x in input2:
price = int(x)
dictionary = {"house":x,"price":price}
house.append(dictionary)
def myFunc(e):
return e['price']
house.sort(key=myFunc)
spent = 0
purchased = 0
for x in house:
variable1 = x.get('price')
spent += variable1
if spent <= B:
purchased += 1
print(f"Case #{test+1}: {purchased}")