三种算法求最大子段和问题——Java实现
来源:互联网 发布:宽带网络布线 编辑:程序博客网 时间:2024/06/06 20:03
- 给定由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<n;i++)
- {
- a[i]=in.nextInt();
- }
- double startTime=System.currentTimeMillis();//starttime
- /**
- *求解最大子段和存在maxSum中
- */
- int maxSum=a[0];
- for(int i=0;i<n-1;i++)
- {
- int temp=a[i];
- for(int j=i+1;j<n;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<leftsum) sum=leftsum;
- //合并,在sum、leftsum和rightsum中取较大者
- if (sum<rightsum) sum=rightsum; }
- 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<n;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<right;j++)
- {
- rights+=a[j];
- if(rights>s2)
- s2=rights;
- }
- maxSum=s1+s2;
- if(maxSum<maxSum1)
- maxSum=maxSum1;
- if(maxSum<maxSum2)
- 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<n;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<n;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<n;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!");
- }
- }
0 0
- 三种算法求最大子段和问题——Java实现
- 三种算法求最大子段和问题——Java实现
- 三种算法实现最大子段和问题(Java实现)
- 动态规划算法求最大子段和问题
- 二分法求最大子段和问题
- 0013算法笔记——【动态规划】最大子段和问题,最大子矩阵和问题,最大m子段和问题
- 0013算法笔记——【动态规划】最大子段和问题,最大子矩阵和问题,最大m子段和问题
- 0013算法笔记——【动态规划】最大子段和问题,最大子矩阵和问题,最大m子段和问题
- 求最大子数组/子序列/子段和问题
- 算法设计与分析--求最大子段和问题(蛮力法、分治法、动态规划法) C++实现
- 【算法设计】最大子段和问题
- 【算法】最大子段和问题
- [算法]最大子段和问题
- 求最大子段和
- 求最大子段和
- 求最大连续子段和 的 dp算法
- 求数组最大子段和的常用算法
- 求最大连续子段和 的 dp算法
- 软件调试学习摘录
- UVA - 10161 Ant on a Chessboard
- HTTP: POST PUT
- 标准光源箱的使用方法和注意事项
- UESTC - 994 两个圆的公共面积
- 三种算法求最大子段和问题——Java实现
- 配置 SQL Server 2005 远程连接(服务器端)
- CodeForces 166E Tetrahedron
- AbstractQueuedSynchronizer(十一)——应用之CountDownLatch
- 编程题目:格式化名字字符串
- AngularJS 表达式
- 开花_纪中1742_线段树
- mac opengl glutInit编译出错
- 省市区三级联动-------xml