Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用合并排序计数反转-意外结果_Php_Mergesort - Fatal编程技术网

Php 使用合并排序计数反转-意外结果

Php 使用合并排序计数反转-意外结果,php,mergesort,Php,Mergesort,这段代码为一小部分元素生成正确的结果,但我不知道为什么对一大部分数字(100000个元素)的结果不正确。比如说这是,。我已经从python代码中得到了正确的结果。但是我想弄明白为什么这个php代码不正确。输出是2397819672 intead的2407905288 $raw_input = file_get_contents($argv[1]); $arr_input = explode("\n",trim($raw_input)); $count = 0.0; function mer

这段代码为一小部分元素生成正确的结果,但我不知道为什么对一大部分数字(100000个元素)的结果不正确。比如说这是,。我已经从python代码中得到了正确的结果。但是我想弄明白为什么这个php代码不正确。输出是2397819672 intead的2407905288

$raw_input = file_get_contents($argv[1]);

$arr_input = explode("\n",trim($raw_input));

$count = 0.0;

function merge_sort($a)
{
    if(count($a) <2) return $a;
    $hl = count($a)/2;
    return merge(merge_sort(array_slice($a, 0, $hl)), merge_sort(array_slice($a, $hl)));

}

function merge($l, $r)
{
    global $count;
    $result = array();
    $i = $j = 0;

    while($i < sizeof($l) && $j < sizeof($r))
    {
        if($l[$i] < $r[$j])
        {
            $result[] =$l[$i];
            $i++;
        }
        else
        {
            $result[] =$r[$j];
            $count+= (sizeof($l) - $i);
            $j++;
        }
    }

    while($i <sizeof($l))
    {
        $result[] = $l[$i];
        $i++;
    }

    while($j <sizeof($r))
    {
        $result[] = $r[$j];
        $j++;
    }
    return $result;
}

$sorted = merge_sort($arr_input);

print $count . "\n";
$raw\u input=file\u get\u contents($argv[1]);
$arr_input=explode(“\n”,trim($raw_input));
$count=0.0;
函数合并\排序($a)
{

如果(count($a),我敢打赌您在PHP中达到了最大整数值

根据官方文件:

整数的大小取决于平台,尽管通常的最大值约为20亿(即32位有符号).64位平台的最大值通常约为9E18。PHP不支持无符号整数。整数大小可以使用常量PHP_INT_size来确定,最大值可以使用常量PHP_INT_MAX来确定,因为PHP 4.4.0和PHP 5.0.5都是如此。

因此您可以更改int_max常量


还有一些未经测试的东西:将其用作字符串。

我敢打赌,您一定达到了PHP中的最大整数值

根据官方文件:

整数的大小取决于平台,尽管通常的最大值约为20亿(即32位有符号).64位平台的最大值通常约为9E18。PHP不支持无符号整数。整数大小可以使用常量PHP_INT_size来确定,最大值可以使用常量PHP_INT_MAX来确定,因为PHP 4.4.0和PHP 5.0.5都是如此。

因此您可以更改int_max常量


还有一些未经测试的东西:将其用作字符串。

我认为这不是一个与最大整数值相关的问题,因为我在python中也遇到过这个问题。 如果代码的最后一部分是

f = open('IntegerArray.txt')
unsorted = list()
for line in f:
    unsorted.append(line)
merge_sort(unsorted)
f = open('IntegerArray.txt')
unsorted = list()
for line in f:
    unsorted.append(int(line))
merge_sort(unsorted)
如果代码的最后一部分是

f = open('IntegerArray.txt')
unsorted = list()
for line in f:
    unsorted.append(line)
merge_sort(unsorted)
f = open('IntegerArray.txt')
unsorted = list()
for line in f:
    unsorted.append(int(line))
merge_sort(unsorted)

你能找出区别吗?这就是答案所在。

我认为这不是一个与最大整数值相关的问题,因为我在python中也遇到过这个问题。 如果代码的最后一部分是

f = open('IntegerArray.txt')
unsorted = list()
for line in f:
    unsorted.append(line)
merge_sort(unsorted)
f = open('IntegerArray.txt')
unsorted = list()
for line in f:
    unsorted.append(int(line))
merge_sort(unsorted)
如果代码的最后一部分是

f = open('IntegerArray.txt')
unsorted = list()
for line in f:
    unsorted.append(line)
merge_sort(unsorted)
f = open('IntegerArray.txt')
unsorted = list()
for line in f:
    unsorted.append(int(line))
merge_sort(unsorted)

你能找出区别吗?这就是答案所在。

与上面的一个答案类似,我使用的是Python。错误是你的代码可能在进行字符串比较,而不是int比较。例如,在Python中,“33”<“4”是真的。

与上面的一个答案类似,我使用的是Python。错误是你的代码可能是在做字符串比较,而不是int比较。例如,在Python中,“33”<“4”是正确的。

我认为是这样,但PHP_int_大小小于2397819672。这就是为什么我感到困惑的原因。
PHP_int_MAX
我想你需要检查一下constantIn我的计算机,它是2147483647:-?但它比我认为的错误结果要小所以,但是PHP_INT_的大小小于2397819672。这就是我感到困惑的原因。
PHP_INT_MAX
我想你需要检查一下康斯坦丁我的计算机,它是2147483647:-?但它比假结果小