最大的数组/比较数组(新视图?) [英] Largest array / Comparing arrays (fresh view?)

查看:58
本文介绍了最大的数组/比较数组(新视图?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能以新鲜的视角观看它吗?我找不到我的错误。

Could someone watch it with a fresh view? I can't find my mistake.

练习和示例:

Input:
4 4
1 4 5 7
10 4 6 8

前两个数字:1)产品数量2)客户数量。下一行:产品价格。第三行:客户所拥有的金额。
脚本必须向其客户推荐价格范围最昂贵的产品。每次输入的内容都不相同

First two numbers: 1) amount of products 2) amount of customers. Next line: prices of products. Third line: amount of money customers have. Script has to recommend the most expensive product for customers in their range of money. Inputs are different each time

Output:
7 4 5 7

我有一个这部分代码有问题:

I'm having a problem with that part of the code:

int largest = price[0];

for(int i = 1; i < money.length; i++) {
    for(int a = 1; a < price.length; a++) {
        if(price[a] > largest && largest <= money[i]) {
            largest = price[a];
            System.out.print(largest + " ");
        }
    } 
}

必要时提供完整代码

import java.io.*;
import java.util.*;
class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String[] sizes = scan.nextLine().split(" ");
        int[] price = new int[Integer.parseInt(sizes[0])];
        int[] money = new int[Integer.parseInt(sizes[1])];

        String[] inputprice = scan.nextLine().split(" ");
        for (int i = 0; i < price.length; i++) {
            price[i] = Integer.parseInt(inputprice[i]);
        }

        String[] inputmoney = scan.nextLine().split(" ");
        for (int i = 0; i < money.length; i++) {
            money[i] = Integer.parseInt(inputmoney[i]);
        }
        scan.close();

        int largest = price[0];
        for (int i = 1; i < money.length; i++) {
            for (int a = 1; a < price.length; a++) {
                if (price[a] > largest && largest <= money[i]) {
                    largest = price[a];
                    System.out.print(largest + " ");
                }
            } 
        }
    }
}

编辑:
仍然给出了错误的答案,但是有了改进:

Still gives bit wrong answer, but there has been an improvement:

for(int i = 0; i < money.length; i++) {
      int largest = price[0];
      for(int a = 0; a < price.length; a++){
        if(price[a] > largest && largest <= money[i]){
          largest = price[a];
        }
      }
      System.out.print(largest+ " ");
    }

给出答案:

Output:
7 4 7 7 

编辑:
代码似乎可以工作,但是在将代码发布到codeforce时会抛出运行时错误。

推荐答案

最新版本有两个错误:


  • 初始化 int最大= price [0]; 是不正确的。您应该初始化为可能的最低值,否则最大可能无效(错误地过高)。您可以将其设置为 Integer.MIN_VALUE

  • 条件 price [a]>最大和最大最大< = money [i] 不正确,应该是 price [a]>最大和最大price [a]< = money [i]

  • The initialization int largest = price[0]; is not correct. You should initialize to the lowest possible value, otherwise largest may be invalid (incorrectly too high). You could set it to Integer.MIN_VALUE.
  • The condition price[a] > largest && largest <= money[i] is incorrect, it should be price[a] > largest && price[a] <= money[i]

另外,最好使用每个循环:

Also, it would be better to use a for-each loop:

for (int m : money) {
    int largest = Integer.MIN_VALUE;
    for (int p : price) {
        if (p > largest && p <= m) {
            largest = p;
        }
    }
    System.out.print(largest + " ");
}

请注意,该算法的性能为 O( m * p)
,其中 m money 的长度,并且 p 价格的长度。
您可以通过对价格 O(p log p)+ O(m * log p) >并使用二进制搜索找到最高价格。

Note that the performance of this algorithm is O(m * p), where m is the length of money and p is the length of price. You could make it O(p log p) + O(m * log p) by sorting price and using binary search to find the highest price.

这篇关于最大的数组/比较数组(新视图?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆