<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>输入一个整形数组,求数组中连续的子数组使其和最大</title>
</head>
<body>
<script>
var arr = [31,-41,59,26,-53,58,97,-93,-23,84];//定义一个数组
function getMaxSum(array){
var arr = [];//定义一个空数组
var sum; //定义和
for(var i=0;i<array.length;i++){ //循环遍历传进来的数组
sum = 0;
for(var j=i;j<array.length;j++){//内层循环
var item = {}; //定义空对象
item['from'] = i; //从哪开始
item['to'] = j;//从哪结束
sum += array[j];//求和
item['sum'] = sum;//sum的值
arr.push(item);
}
console.log(arr)
}
return arr.sort(function(a, b){return b.sum - a.sum})[0] //b-a取第一个就是最大值
}
console.log(getMaxSum(arr));
</script>
</body>
</html>