/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package maxsum;
class sum
{
int array1[];
int arrayend[]={0,0,0};
public sum(){}
public sum(int i){
array1=new int[i];
for(int j=0;j<array1.length;j++)
{
array1[j] = (int) (Math.random() * 26)-10;
System.out.print(array1[j]+" ");
}
}
public void findMaxSumCM() {
int head = 0;
int sum = 0;
int arrayend[] = {0, 0, 0};//保存最大值,起点,终点;
System.out.println();
for (int i = 0; i < array1.length; i++) {
sum = sum + array1[i];
if (sum < 0)//丢弃
{
sum = 0;
if (i != array1.length - 1) //如果下标到头,则不做任何处理
//如果下小不到头
{
head = i + 1;
}
} else//处理
{
if (sum > arrayend[0])//如果此sum>此前存的值,则替换之。
{
arrayend[0] = sum;
arrayend[1] = head;
arrayend[2] = i;
}
}
}
System.out.print("\n");
System.out.println("sum=" + arrayend[0] + " 下标= " + arrayend[1] + "终点= " + arrayend[2]);
}
}
/**
*
* @author Administrator
*/
public class MaxSum {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
sum sm=new sum(10);
sm.findMaxSumCM();
}
}