您的位置:首页 > Web前端 > JavaScript

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

2011-10-29 20:08 295 查看
<!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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: