子序列求和最大值之javascript实现
来源:互联网 发布:免费网络英语 编辑:程序博客网 时间:2024/05/17 01:52
<!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>