简体   繁体   English

我已经尝试使用long作为函数的变量类型以及返回类型,但是仍然溢出。 我不明白为什么?

[英]I have tried using long both as the variable type as well as the return type of the function but still it is overflowing. I cannot understand why?

I am calling the getMaxPairwiseProduct() method and but it is returning overflow value of the "product" variable. 我正在调用getMaxPairwiseProduct()方法,但它返回“产品”变量的溢出值。 I am already using the long and the product actually is in the limits of long but I cannot understand why it is returning overflow value. 我已经在使用多头了,产品实际上在多头的极限内,但是我不明白为什么它返回溢出值。

import java.util.Scanner;
public class MaxPairwiseProduct {

    static long getMaxPairwiseProduct(int[] array){
        //int product =0 ;
        int n = array.length;
        //for(int j =0; j<n ; ++j){
        //}
        QuickSort(array, 0, array.length -1 );
        int n1 = (array[array.length-1]);
        int n2 = (array[array.length-2]);
         long product =n1 * n2;  
         System.out.println(product);
         return product;
}

        private static void QuickSort(int[] arr,  int left, int right){
            int index = partition(arr,left, right);
            if(left < index - 1)
                QuickSort(arr, left, index -1);
            if(index < right)
                QuickSort(arr, index, right);

            //System.out.println(index);
        }
    private static int partition(int[] arr, int left, int right){
        int pivot = arr[(left + right )/2];
        while(left<=right){
            while(arr[left] < pivot) left++;
            while(arr[right] > pivot) right--;

            if(left<= right){
                int temp= arr[left];
                arr[left] = arr[right];
                arr[right]  =temp;

                left++;
                right--;
            }
        }
        return left;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        /*int n = sc.nextInt();
          int[] array = new int[n];
        for(int i=0; i <n;++i){
          array[i] = sc.nextInt();
         }
         */
        int[] array = new int[]{100000, 90000};
         long product = getMaxPairwiseProduct(array);
         System.out.println(product);
    }
}

Long story short, in Java, every integer (non-decimal) number is treated as int by default, your line: 长话短说,在Java中,默认情况下,每个整数(非十进制)数字都被视为int ,您的行:

long product =n1 * n2;  

Will multiply two int values which will overflow and only once the overflowed result is obtained it will be cast to long and saved in product variable. 将两个将溢出的int值相乘,只有获得溢出的结果后,它才会转换为long并保存在product变量中。

Fix ? 解决?

Cast one of your two operands to long explicitly, something like this would work: 将两个操作数之一显式转换为long ,这样可以正常工作:

long product = (long)n1 * n2;  

Java will automatically start working with "biggest" data type, so since you are multiplying long and int , the int or n2 will be implicitly converted to long as well, no overflow will occur and you should get correct result. Java将自动开始使用“最大”数据类型,因此,由于您将longint相乘,因此intn2也将隐式转换为long ,因此不会发生溢出,并且应该得到正确的结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么这个长溢出为-1,而不是类型的最小值? - Why is this long overflowing to -1, instead of the minimum value for the type? 为什么仍然需要错误返回类型? - Why there still have error return type required? 为什么返回类型不是我在这里编码的? - Why isn't the return type what I have coded here? 不明白为什么不能将此变量转换为类型? (Java) - Don't understand why this variable cannot be resovled to a type? (Java) 为什么 long 类型的变量在转换之后和转换之前在以下代码中给出不同的答案,因为两者都是 long 类型? - Why variable of long type is giving different answer after casting and before casting in the following code as both are of type long? 我不明白在尝试使此数组问题起作用时应该使用的特定变量类型或利用率类型 - I don't understand the specific variable type or type of utilization I should have when trying to get this array issue to work 为什么我仍然可以输入这个特殊字符“`”、“_”和“^” - why i can still type this special characters " ` " , " _" and " ^ " 当声明变量具有函数作用域时,我得到类型的错误 - I get an error of the type when the variable is declared to have a function scope 我怎么能指定一个匹配String和long的类型? - How could I specify a type that match both String and long? 我想在Java中将枚举类型变量转换为长类型 - I want to convert an enum type variable to long type in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM