当前位置:编程学习 > JAVA >>

子序列求和最大值之javascript实现

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 

<head> 

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 

<title>无标题文档</title> 

</head> 

 

<body> 

<script type="text/javascript"> 

var list = [4,2,1,-2,10,90,40,-93,-1,8,70]; 

function subSeqMax(a){ 

       var tempMax =0; 

       var currMax =0; 

       for(var i=0;i<a.length;i++){ 

           for(var j=i;j<a.length;j++){ 

               tempMax =addTo(i,j,a); 

               if(tempMax>currMax){ 

                   currMax = tempMax; 

               } 

           } 

       } 

       return currMax; 

 

function addTo(start,end,a){ 

    var tempMax =0; 

    var currMax =0; 

    while(start<=end){ 

        tempMax +=a[start++]; 

        if(tempMax>currMax){ 

            currMax = tempMax;//如果比储存的值大,就刷新之; 

        } 

    } 

    return currMax; 

 

alert(subSeqMax(list)); 

//addTo(0,list.length-1,list) 

</script> 

</body> 

</html>   

 

摘自 用文字记录生命

补充:web前端 , JavaScript ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,