如何将 java 中的两个 long 值相乘

How to multiply two long values in java

我正在尝试将一个数字数组中的两个最大数字相乘。它对小数字工作正常。

正确的输入/输出 - 这是有效的:

3 10 2 8 80

正确的输入/输出 - 这是失败的:

2 100000 90000
9000000000

然而我的输出是 10000000000。

有人能告诉我我的代码有什么问题吗?

public static Long sumPairwise(Long[] numbers){

        int index=0;
        int n = numbers.length;
        for(int i=1;i<n;i++){
            if(numbers[i]>numbers[index])
                    index=i;
        }
        numbers[n-1]= numbers[index];
        index=0;
        for(int j=1;j<n-1;j++){
        if(numbers[j]>numbers[index])
                index=j;
        }
        numbers[n-2]=numbers[index];
        Long product = (numbers[n-2])*(numbers[n-1]);

    return product ;
}
public static void main(String [] args){
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    Long numbers[] = new Long[n];
    for (int i=0;i<n;i++)
    numbers[i]= sc.nextLong();
    System.out.println(sumPairwise(numbers));

}

而不是 Long 尝试使用 BigInteger 乘以适合 long 的较大值,否则您的结果可能 溢出

使用 BigDecimal 代替浮点数相乘。

您正在将该索引中的原始号码替换为另一个号码。 这就是导致问题的原因。

请简单地从下面的逻辑中找出最多 2 个数字并相乘。 另外,记得关闭扫描仪。

这是简单的解决方案。这仅适用于正整数。

import java.util.Scanner;

    public class Snippet {
        public static long multiplyHighestTwoValues(Long[] numbers) {

            long maxOne = 0;
            long maxTwo = 0;
            for (long n : numbers) {
                if (maxOne < n) {
                    maxTwo = maxOne;
                    maxOne = n;
                } else if (maxTwo < n) {
                    maxTwo = n;
                }
            }

            long product = maxOne * maxTwo;
            return product;
        }

        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            Long numbers[] = new Long[n];
            for (int i = 0; i < n; i++)
                numbers[i] = sc.nextLong();
            System.out.println(sumPairwise(numbers));
            sc.close();

        }
    }

您的代码中存在错误:numbers[n-1] 可能包含第二大数字。在尝试将它放在倒数第一个位置之前,您正在用代码中的最高数字覆盖该数字。

克服这个问题的一种方法是使用 Arrays.sort 对数组进行排序,这样您就可以确定最后两个数字是最高和第二高的数字。

public static long multiplyLargestTwoNumbers(long[] numbers) {
    long[] sortedNumbers = numbers.clone();
    Arrays.sort(sortedNumbers);

    int size = numbers.length;
    // multiply highest and second highest number
    return sortedNumbers[size - 1] * sortedNumbers[size - 2];
}

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    long numbers[] = new long[n];
    for (int i = 0; i < n; i++) {
        numbers[i] = sc.nextLong();
    }
    System.out.println(multiplyLargestTwoNumbers(numbers));
}

其他变化:

  • 使用 long 而不是 Long:当不需要 objective 引用类型时尝试使用基本类型(如果你想使用例如你需要 Long a List 因为 a List 只能保存对象引用);
  • spaced出for循环,请用白色space;
  • 重命名方法,因为它不成对添加任何东西;
  • 在 main 方法中 for 循环使用花括号;
  • 删除了执行乘法部分的虚假括号。

您还可以引入一个 if 语句,该语句首先检查 numbers 数组是否确实包含至少两个元素。这称为 guard 语句.

最后记住 byteshortlong 都包含特定位大小的有符号数。基本上你正在执行计算 modulus 2^n 其中 n 是位大小。如果该值太大,它可能会溢出并 return 一个不正确的结果。为此你需要 BigInteger.