求最大和 java_三种算法求最大子段和问题——Java实现

本文介绍了三种Java实现方法来求解最大子段和问题,包括蛮力算法、分治算法和动态规划算法。通过具体代码示例展示了如何在不同情况下找到整数序列中的最大子段和,当所有整数都为负数时,最大子段和为0。
摘要由CSDN通过智能技术生成

给定由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

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2010-03-14 22:21

浏览 4309

评论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值