标签:style blog io ar java for sp on 2014
问题:给指定的序列划分为3份,取每份的最大值,再从3份最大值取出最大的max。如何划分,可以使max最小?Array.prototype.sum = function(i,j){ i = i == undefined ? 0 : i; j = j == undefined ? 0 : j; var s = 0; for(var k = i;k <=j ;k++){ s+=this[k]; } return s; } function f(arr){ var result = {val:undefined,p1:"",p2:""}; for(var p1 = 0;p1<arr.length-2;p1++){ var s1 = arr.sum(0,p1); for(var p2 = p1+1;p2<arr.length-1;p2++){ var s2 = arr.sum(p1+1,p2); var s3 = arr.sum(p2+1,arr.length-1); var min = Math.max(Math.max(s1,s2),s3); if(p1==2){ console.log("min : "+min +",p1:"+p1+",p2:"+p2+",s2:"+s2+",s3:"+s3); } if(!result.val || result.val > min){result.val = min;result.p1 = p1;result.p2=p2;} } } return result; } console.log(f(new Array(1,2,3,4,5)));
标签:style blog io ar java for sp on 2014
原文地址:http://blog.csdn.net/lan_liang/article/details/40581801