不必要的新行 php

我有一个 PHP 代码。该代码应该打印输出后跟一个新行。代码工作正常,但我最后有不必要的新行。最后应该只有一个换行符,但我的代码打印了几行新行。可能是什么问题?请帮忙。


<?php

  /* Read input from STDIN. Print your output to STDOUT*/

  $fp = fopen("php://stdin", "r");

  //Write code here

  $loop = 0;

  $n = 0; $arr = [];

  while(!feof($fp)) {

    $arr = []; $n = 0;

    if($loop == 0) {

      $total = fgets($fp);

    }

    else {

      if($loop%2 == 1) {

        $n = fgets($fp);

      }

      else {

        $arr = fgets($fp);

      }

    }

    if($loop > 0 && $loop%2 == 0) {

      $arr = explode(" ", $arr);

      $m = [];

      for($i = 0; $i < 1<<10; $i++) {

        $m[$i] = -1;

      }

      $n = count($arr);


      $r = 0;

      for($i = 0; $i < 1<<10; $i++) {

        $r = max($r, fd_sum($i, $m, $arr, $n));

      }


      echo $r."\n";

    }


    $loop++;

  }

  fclose($fp);


?>


<?php

  function fd_sum($i, $m, $arr, $n) {

    if($i == 0) {

      return $m[$i] = 0;

    }

    else if($m[$i] != -1) {

      return $m[$i];

    }

    else {

        $rr = 0;

        for($j = 0; $j < $n; $j++) {

          $num = (int)$arr[$j];

          $b = save($num);

          if(($i | $b) == $i) {

              $z = $i^save($num);

              $y = fd_sum($z, $m, $arr, $n);

              $v = ($y + $num);

            $rr = max($v, $rr);

          }


        }

        return $m[$i] = $rr;

    }

  }

?>


<?php

  function save($nm)

  {

    $x = 0;

    for($i = 1; $nm/$i > 0; $i *= 10) {

      $d = ($nm/$i) % 10;

      $x = $x | (1 << $d);

    }

    return $x-1;

  }


?>

我的输入是


3

4

3 5 7 2

5

121 3 333 23 4

7

32 42 52 62 72 82 92

我的输出是


17

458

92

-

-

-

-

预期的输出是


17

458

92

-

注意:我使用了“-”来表示新行


我究竟做错了什么?请帮忙。


繁星淼淼
浏览 150回答 1
1回答

婷婷同学_

PHP 解释器正在读取结束标记后的新行,然后将其作为输出立即吐出。删除额外的开始/结束标签应该删除额外的新行。此外,php 结束标记不是必需的,我建议省略它们。<?php&nbsp; /* Read input from STDIN. Print your output to STDOUT*/&nbsp; $fp = fopen("php://stdin", "r");&nbsp; //Write code here&nbsp; $loop = 0;&nbsp; $n = 0; $arr = [];&nbsp; while(!feof($fp)) {&nbsp; &nbsp; $arr = []; $n = 0;&nbsp; &nbsp; if($loop == 0) {&nbsp; &nbsp; &nbsp; $total = fgets($fp);&nbsp; &nbsp; }&nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; if($loop%2 == 1) {&nbsp; &nbsp; &nbsp; &nbsp; $n = fgets($fp);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; $arr = fgets($fp);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if($loop > 0 && $loop%2 == 0) {&nbsp; &nbsp; &nbsp; $arr = explode(" ", $arr);&nbsp; &nbsp; &nbsp; $m = [];&nbsp; &nbsp; &nbsp; for($i = 0; $i < 1<<10; $i++) {&nbsp; &nbsp; &nbsp; &nbsp; $m[$i] = -1;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; $n = count($arr);&nbsp; &nbsp; &nbsp; $r = 0;&nbsp; &nbsp; &nbsp; for($i = 0; $i < 1<<10; $i++) {&nbsp; &nbsp; &nbsp; &nbsp; $r = max($r, fd_sum($i, $m, $arr, $n));&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; echo $r."\n";&nbsp; &nbsp; }&nbsp; &nbsp; $loop++;&nbsp; }&nbsp; fclose($fp);&nbsp; function fd_sum($i, $m, $arr, $n) {&nbsp; &nbsp; if($i == 0) {&nbsp; &nbsp; &nbsp; return $m[$i] = 0;&nbsp; &nbsp; }&nbsp; &nbsp; else if($m[$i] != -1) {&nbsp; &nbsp; &nbsp; return $m[$i];&nbsp; &nbsp; }&nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; $rr = 0;&nbsp; &nbsp; &nbsp; &nbsp; for($j = 0; $j < $n; $j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $num = (int)$arr[$j];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $b = save($num);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(($i | $b) == $i) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $z = $i^save($num);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $y = fd_sum($z, $m, $arr, $n);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $v = ($y + $num);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $rr = max($v, $rr);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return $m[$i] = $rr;&nbsp; &nbsp; }&nbsp; }&nbsp; function save($nm)&nbsp; {&nbsp; &nbsp; $x = 0;&nbsp; &nbsp; for($i = 1; $nm/$i > 0; $i *= 10) {&nbsp; &nbsp; &nbsp; $d = ($nm/$i) % 10;&nbsp; &nbsp; &nbsp; $x = $x | (1 << $d);&nbsp; &nbsp; }&nbsp; &nbsp; return $x-1;&nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP