【问题标题】:Google KickStart Wrong AnswerGoogle KickStart 错误答案
【发布时间】:2020-03-22 06:39:05
【问题描述】:

我为 Google Kickstart 2020 Round A 制作了一个程序,但它显示错误答案并跳过了测试集。 这就是问题

问题

有 N 栋房屋待售。第 i 间房子花费 Ai 美元购买。您的预算为 B 美元。

你最多可以买多少房子? 输入

输入的第一行给出了测试用例的数量,T.T 测试用例紧随其后。每个测试用例都以包含两个整数 N 和 B 的单行开始。第二行包含 N 个整数。第 i 个整数是 Ai,即第 i 个房子的成本。 输出

对于每个测试用例,输出一行包含 Case #x: y,其中 x 是测试用例编号(从 1 开始),y 是您可以购买的最大房屋数量。 限制

时间限制:每个测试集 15 秒。 内存限制:1GB。 1≤T≤100。 1≤B≤105。 1 ≤ Ai ≤ 1000,对于所有 i。 测试集 1

1 ≤ N ≤ 100。 测试集 2

1 ≤ N ≤ 105。 样本

输入

3 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 美元购买 1 号和 3 号房屋。 在示例案例 #2 中,您的预算为 50 美元。您可以花 30 + 10 + 10 = 50 美元购买第 1、第 3 和第 4 栋房屋。 在示例案例 #3 中,您的预算为 300 美元。你不能买任何房子(所以答案是0)。

注意:与以前的版本不同,在 Kick Start 2020 中,所有测试集都是可见的判定测试集,这意味着您在提交时会收到即时反馈。

我的代码是

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

只是帮助我这有什么问题.. 谢谢!

This is what they say

【问题讨论】:

  • 这只是被问到25 minutes ago。你能告诉我们你的算法吗?什么测试失败了?
  • 他们没有显示任何其他细节
  • 您可以监视该线程以获取问题的答案,但算法看起来相同。希望他们会对其进行编辑以添加详细信息。

标签: python


【解决方案1】:

您的代码的问题是您是否可以购买所有不打印答案的房屋,因为您只有在总成本超过预算时才打印。这可以通过在程序结束时将 print 语句移出循环来轻松解决。

【讨论】:

  • 我先把它放在外面。但它仍然有同样的问题,所以我把它放在里面,仍然是那样......
  • 我只是尝试在循环之后使用打印语句提交它,它通过了测试用例。您是否记得将 break 语句保留在循环中?
【解决方案2】:
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}")

通过了样本和两项测试

【讨论】:

    【解决方案3】:
     #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;
    }
    }
    

    【讨论】:

    • 如果您也能解释一下您的算法,那就太好了。
    • 您能否提供一些解释,以便其他人可以投票给您?
    【解决方案4】:

    tt = int(输入()) 对于范围(tt)中的测试用例: 数字,预算=列表(地图(int,输入().split())) 打印(数量,预算)

    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)
        
    

    【讨论】:

    • 嗨 Himanshu,欢迎来到 Stackoverflow。虽然您的贡献非常受欢迎,但很难理解您的代码实际上对问题做了什么。请解释它的各个部分,以及它们如何解决最初的问题。
    【解决方案5】:

    KickStart 2020 Round A(分配问题解决方案)使用 JAVA

    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;    
        }
    }
    
    

    ScreenShot of solution

    【讨论】:

      【解决方案6】:
      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)
      

      【讨论】:

        【解决方案7】:
        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)
        

        【讨论】:

        • 嗨 Mukund,欢迎来到 SO。一般来说,人们不喜欢没有解释的答案。请在答案中添加一些解释。谢谢
        猜你喜欢
        • 1970-01-01
        • 2023-03-25
        • 1970-01-01
        • 2020-11-21
        • 2019-09-28
        • 2020-02-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多