Write a PHP function. To find the largest continuous sum among any n positive and negative integers, the time complexity of the algorithm is required to be as low as possible.

WBOY
Release: 2016-07-28 08:27:46
Original
1368 people have browsed it

header("content-type:text/html;charset=utf8");
//Algorithm analysis:
//1. It must be a sequence of integers
//2. If the entire sequence is not all negative numbers, the maximum The first item of the subsequence must be a positive number,
//Otherwise, if the numbers after the largest subsequence are added together with the negative number of the first item, the sum will definitely not be the largest;
//3. If the entire sequence is negative, Then the sum of the maximum subsequence is 0;
                   $arr=array(-2,1,3,9,-4,2, 3,8,-3,-4, 1,3);                        $thissum=0;
            $maxsum=0;
             $start=0;//Record the starting subscript of the subsequence
          $end=0;//                  $maxsum=0; $arr);$i++){
                                                                                                                             The sum of the sum of An element value is assumed to be the first item of the largest subsequence. Here it can be guaranteed that the first item of the largest self-sequence must be a positive number
            $thissum=0;//Provided that this sequence is not all negative numbers
                                                                                                                                                      }
                                                                 $parr=array($start,$end,$maxsum); ;';
  echo 'The maximum subsequence is:';
  for($i=$start;$i<=$end;$i++){
   echo $arr[$i].' ';
  }
  echo '
';
echo 'The sum of the maximum subsequence is'.$maxsum;
?>

The above introduces writing a PHP function. To find the largest continuous sum among any n positive and negative integers, the time complexity of the algorithm is required to be as low as possible. , including relevant content, I hope it will be helpful to friends who are interested in PHP tutorials.


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!