给定由n个整数组成的序列(a1, a2, …, an),求该序列的子段和的最大值,当所有整数均为负整数时,其最大子段和为0。
LargestSubsegmentSum1.java //蛮力算法
import java.util.*;
public class LargestSubsegmentSum1
{
public static void main(String[] args)
{
/**
*从键盘输入所要求的序列的长度n
*/
Scanner in=new Scanner(System.in);
System.out.println("Please enter the length of segment you want to make(输入你要求的序列的长度):");
int n=in.nextInt();
/**
*从键盘输入所要求的序列,存储在a[n]中
*/
int[] a=new int[n];
System.out.println("Now,please enter the elements of the segment you want(现在请依次输入这个序列包含的元素(整数)):");
for(int i=0;i
{
a[i]=in.nextInt();
}
double startTime=System.currentTimeMillis();//starttime
/**
*求解最大子段和存在maxSum中
*/
int maxSum=a[0];
for(int i=0;i
{
int temp=a[i];
for(int j=i+1;j
{
temp+=a[j];
if(temp>maxSum)
maxSum=temp;
}
}
double endTime=System.currentTimeMillis();//endtime
/**
*打印输出求解结果和程序所用时间
*/
System.out.println("The largest sub-segment sum is(最大子段和是):"+maxSum);
System.out.println("Basic Statements take(基本语句用时) "+(endTime-startTime)+" milliseconds!");
}
}
***************************************************************************
分治算法描述:
int MaxSum(int a[ ], int left, int right)
{
sum=0;
if (left= =right) { //如果序列长度为1,直接求解
if (a[left]>0) sum=a[left];
else sum=0;
}
else {
center=(left+right)/2; //划分
leftsum=MaxSum(a, left, center);
//对应情况①,递归求解
rightsum=MaxSum(a, center+1, right);
//对应情况②,递归求解
s1=0; lefts=0; //以下对应情况③,先求解s1
for (i=center; i>=left; i--)
{
lefts+=a[i];
if (lefts>s1) s1=lefts; }
s2=0; rights=0; //再求解s2
for (j=center+1; j<=right; j++)
{
rights+=a[j];
if (rights>s2) s2=rights; }
sum=s1+s2; //计算情况③的最大子段和
if (sum
//合并,在sum、leftsum和rightsum中取较大者
if (sum
return sum; }
LargestSubsegmentSum2.java //分治算法
import java.util.*;
public class LargestSubsegmentSum2
{
public static void main(String[] args)
{
/**
*从键盘输入所要求的序列的长度n
*/
Scanner in=new Scanner(System.in);
System.out.println("Please enter the length of segment you want to make(输入你要求的序列的长度):");
int n=in.nextInt();
/**
*从键盘输入所要求的序列,存储在a[n]中
*/
int[] a=new int[n];
System.out.println("Now,please enter the elements of the segment you want(现在请依次输入这个序列包含的元素(整数)):");
for(int i=0;i
{
a[i]=in.nextInt();
}
/**
*调用函数MaxSum()求解出结果
*/
double startTime=System.currentTimeMillis();//starttime
int maxSum=MaxSum(a,0,n);
double endTime=System.currentTimeMillis();//endtime
/**
*打印输出结果和程序运行所用时间
*/
System.out.println("The largest sub-segment sum is(最大子段和是):"+maxSum);
System.out.println("Basic Statements take(基本语句用时) "+(endTime-startTime)+" milliseconds!");
}
/**
*设计求解最大子段和的函数MaxSum()
*/
public static int MaxSum(int[] a,int left,int right)
{
int maxSum=0;
if(left==right)
{
if(a[0]>0)
maxSum=a[0];
}
else
{
int center=(left+right)/2;
int maxSum1=MaxSum(a,left,center);
int maxSum2=MaxSum(a,center+1,right);
int s1=0;
int lefts=0;
for(int i=center;i>=left;i--)
{
lefts+=a[i];
if(lefts>s1)
s1=lefts;
}
int s2=0;
int rights=0;
for(int j=center+1;j
{
rights+=a[j];
if(rights>s2)
s2=rights;
}
maxSum=s1+s2;
if(maxSum
maxSum=maxSum1;
if(maxSum
maxSum=maxSum2;
}
return maxSum;
}
}
***********************************************************************************************
LargestSubsegmentSum3.java //动态规划算法
import java.util.*;
public class LargestSubsegmentSum3
{
public static void main(String[] args)
{
/**
*从键盘输入所要求的序列的长度n
*/
Scanner in=new Scanner(System.in);
System.out.println("Please enter the length of segment you want to make(输入你要求的序列的长度):");
int n=in.nextInt();
/**
*从键盘输入所要求的序列,存储在a[n]中
*/
int[] a=new int[n];
System.out.println("Now,please enter the elements of the segment you want(现在请依次输入这个序列包含的元素(整数)):");
int i;
for(i=0;i
{
a[i]=in.nextInt();
}
/**
*求解最大子段和存在maxSum中
*/
double startTime=System.currentTimeMillis();//starttime
int maxSum=0;
int[] b=new int[n];
b[0]=0;
for(int j=1;j
{
if(b[j-1]>0)
b[j]=b[j-1]+a[j];
else
{
b[j]=a[j];
i=j;
}
}
for(int j=0;j
{
if(b[j]>maxSum)
maxSum=b[j];
}
double endTime=System.currentTimeMillis();//endtime
/**
*打印输出结果和程序运行所用时间
*/
System.out.println("The largest sub-segment sum is(最大子段和是):"+maxSum);
System.out.println("Basic Statements take(基本语句用时) "+(endTime-startTime)+" milliseconds!");
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wanbingglass/archive/2009/12/22/5057194.aspx
分享到:
2010-03-14 22:21
浏览 4309
评论