简体   繁体   English

为100,000个整数的数组计算反转,为什么要得到负输出?

[英]Counting Inversions for an Array of 100,000 Integers, Why Get a Negative Output?

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class InversionCounter {
    public static void main(String[] args) {
        Scanner scanner = null;
        try {
            scanner = new Scanner(new File("src/IntegerArray.txt"));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int [] nums = new int [100000];
        int i = 0;
        while(scanner.hasNextInt()){
           nums[i++] = scanner.nextInt();
        }
        System.out.println(countInversions(nums));

    }

public static int countInversions(int[] nums) {
    int count = 0;
    for (int i=0;i<nums.length-1;i++) {
        for (int j=i+1;j<nums.length;j++) {
            if (nums[i]>nums[j]) {
                count++;
            }
            else {continue;}
        }
    }
    return count;
}

}

The code above reads 100,000 integers from a file and counts the inversions for this array of integers. 上面的代码从文件中读取100,000个整数,并计算此整数数组的取反数。 The output is probably a very large number like 1198233847 and should definitely be positive. 输出可能是非常大的数字,例如1198233847,并且绝对应该是正数。 However, it outputs a negative one like -1887062008. 但是,它输出负值,如-1887062008。 The program logic is likely to be correct as I have tried other algorithms for the same purpose and got the same negative number as output. 程序逻辑很可能是正确的,因为我出于相同的目的尝试了其他算法并获得与输出相同的负数。 I suspect that the result is too big a positive number and as a result Java converts it to a negative one. 我怀疑结果太大了,所以Java将其转换为负数。

The max value of an int is 2,147,483,647 - what you are seeing is overflow. 一个int的最大值是2,147,483,647-您看到的是溢出。 You should make count a long if you expect it to be so big. 如果您希望count如此之大,则应long count

source: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html 来源: http : //docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

The worst case here is 4,999,950,000 inversions, which is bigger than int's max value (2,147,483,647). 最坏的情况是4,999,950,000个反转,它比int的最大值(2,147,483,647)大。 You should probably use a long to store the number. 您可能应该使用长号来存储数字。

暂无
暂无

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

相关问题 计算大型数组的平均中位数(最多100,000个元素) - Calculating the average median of a large array (up to 100,000 elements) 同时运行100,000个进程 - Running 100,000 processes concurrently 有谁知道为什么我的快速排序算法在大数据集上出现堆栈溢出错误,例如。 长度为 100,000 的数组? - Does anyone know why my quick sort algorithm is getting a stack overflow error on large data sets ex. an array of length 100,000? 为什么在redis中使用管道时,有100,000条记录这么慢? - why it is so slow with 100,000 records when using pipeline in redis? 当我将报告导出为包含100,000个字符的PDF格式时,我收到SAXException - I get a SAXException when I export a report to PDF format with 100,000 characters 创建具有100,000个数字的1,000个数组 - Creating 1,000 arrays with 100,000 numbers 如果数组的长度为100000,则用归并排序对反转进行计数会得出负数 - counting inversions with merge sort gives a negative number if the array's length is 100000 Mergesort Implementation ..计算数组中的反转次数 - Mergesort Implementation.. Counting number of inversions in an array 提高从数据库加载 100,000 条记录的性能 - Improve performance of loading 100,000 records from database DataInputStream.read() 只读取真正大的 arrays(>100,000 字节)的前几个字节是有原因的吗? - Is there a reason why DataInputStream.read() only reads the first few bytes of really big arrays (>100,000 bytes)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM