最大子段和算法
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
{
if(rand() % 2 == 0)
num[i] = rand();
else
num[i] = (-1) * rand();
if(n < 100)
cout<<num[i]<<" ";
}
cout<<endl;
//蛮力法//
cout<<"\n蛮力法:"<<endl;
cout<"最大字段和:";
QueryPerformanceCounter(&begin);
{
rights += a[j];
if(rights > s2) s2 =rights;
}
sum = s1 + s2;//计算第3钟情况的最大子段和
if(sum < leftsum) sum = leftsum;//合并,在sum、leftsum、rightsum中取最大值
if(sum < rightsum) sum = rightsum;
const int n = 40;
LARGE_INTEGER begin,end,frequency;
QueryPerformanceFrequency(&frequency);
//生成随机序列
cout<<"生成随机序列:";
srand(time(0));
for(int i = 0; i < n; i++)
cout<<BF_Sum(num,n)<<endl;
QueryPerformanceCounter(&end);
cout<<"时间:"
<<(double)(end.QuadPart - begin.QuadPart) / frequency.QuadPart
<<"s"<<endl;
cout<<"\n分治法:"<<endl;
6.ThisSum = 0; /* ThisSum是从A[i]到A[j]的子列和*/
7.for( k = i; k <= j; k++ )
8.ThisSum += A[k];
9.if( ThisSum > MaxSum ) /*如果刚得到的这个子列和更大*/
10.MaxSum = ThisSum; /*则更新结果*/
<<"s"<<endl;
cout<<"\n动态规划法:"<<endl;
cout<"最大字段和:";
QueryPerformanceCounter(&begin);
cout<<DY_Sum(num,n)<<endl;
QueryPerformanceCounter(&end);
cout<<"时间:"
<<(double)(end.QuadPart - begin.QuadPart) / frequency.QuadPart
b[i] = b[i - 1] + a[i];
else
b[i] = a[i];
}
for(int j = 0; j < n; j++)
{
if(b[j] > sum)
sum = b[j];
}
delete []b;//释放内存
return sum;
}
int main()
{
int num[MAX];
int i;
MaxSum = ThisSum; /*则更新结果*/
} /* j循环结束*/
} /* i循环结束*/
return MaxSum;
}
最大子段和nlongn算法(分治)
int maxSum(int a[],int left, int right)
{
int sum = 0;
if(left == right)//如果序列长度为1,直接求解
#include<time.h>
#include<Windows.h>
using namespace std;
#define MAX 10000
int BF_Sum(int a[],int n)
{
int max=0;
int sum=Fra Baidu bibliotek;
int i,j;
for (i=0;i<n-1;i++)
{
sum=a[i];
最大子段和n3算法
1.int MaxSubseqSum1( int A[], int N )
2.{ int ThisSum, MaxSum = 0;
3.int i, j, k;
4.for( i = 0; i < N; i++ ) { /* i是子列左端位置*/
5.for( j = i; j < N; j++ ) { /* j是子列右端位置*/
{
if(b[i-1] > 0)
b[i] = b[i - 1] + a[i];
else
b[i] = a[i];
}
for(int j = 0; j < n; j++)
{
if(b[j] > sum)
sum = b[j];
}
delete []b;//释放内存
return sum;
}
完整测试程序:
#include<iostream>
{
rights += a[j];
if(rights > s2) s2 =rights;
}
sum = s1 + s2;//计算第3钟情况的最大子段和
if(sum < leftsum) sum = leftsum;//合并,在sum、leftsum、rightsum中取最大值
if(sum < rightsum) sum = rightsum;
cout<"最大字段和:";
QueryPerformanceCounter(&begin);
cout<<maxSum1(num,0,n)<<endl;
QueryPerformanceCounter(&end);
cout<<"时间:"
<<(double)(end.QuadPart - begin.QuadPart) / frequency.QuadPart
{
if(a[left] > 0) sum = a[left];
else sum = 0;
}
else
{
int center = (left + right) / 2;//划分
int leftsum = maxSum(a,left,center);//对应情况1,递归求解
int rightsum = maxSum(a, center + 1, right);//对应情况2,递归求解
{
if(a[left] > 0) sum = a[left];
else sum = 0;
}
else
{
int center = (left + right) / 2;//划分
int leftsum = maxSum1(a,left,center);//对应情况1,递归求解
int rightsum = maxSum1(a, center + 1, right);//对应情况2,递归求解
11.} /* j循环结束*/
12.} /* i循环结束*/
13.return MaxSum;
14.}
最大子段和n2算法
int MaxSubseqSum2( int A[], int N )
{ int ThisSum, MaxSum = 0;
int i, j;
for( i = 0; i < N; i++ ) { /* i是子列左端位置*/
int s1 = 0;
int lefts = 0;
for(int i = center; i >= left; i--)//求解s1
{
lefts += a[i];
if(lefts > s1) s1 = lefts;//左边最大值放在s1
}
int s2 = 0;
int rights = 0;
for(int j = center + 1; j <= right; j++)//求解s2
int s1 = 0;
int lefts = 0;
for(int i = center; i >= left; i--)//求解s1
{
lefts += a[i];
if(lefts > s1) s1 = lefts;//左边最大值放在s1
}
int s2 = 0;
int rights = 0;
for(int j = center + 1; j <= right; j++)//求解s2
<<"s"<<endl;
system("pause");
return 0;
}
}
return sum;
}
最大子段和动态规划算法
int DY_Sum(int a[],int n)
{
int sum = 0;
int *b = (int *) malloc(n * sizeof(int));//动态为数组分配空间
b[0] = a[0];
for(int i = 1; i < n; i++)
for(j=i+1;j<n;j++)
{
if(sum>=max)
{
max=sum;
}
sum+=a[j];
}
}
return max;
}
int maxSum1(int a[],int left, int right)
{
int sum = 0;
if(left == right)//如果序列长度为1,直接求解
}
return sum;
}
int DY_Sum(int a[],int n)
{
int sum = 0;
int *b = (int *) malloc(n * sizeof(int));//动态为数组分配空间
b[0] = a[0];
for(int i = 1; i < n; i++)
{
if(b[i-1] > 0)
ThisSum = 0; /* ThisSum是从A[i]到A[j]的子列和*/
for( j = i; j < N; j++ ) { /* j是子列右端位置*/
ThisSum += A[j]; /*对于相同的i,不同的j,只要在j-1次循环的基础上累加1项即可*/
if( ThisSum > MaxSum ) /*如果刚得到的这个子列和更大*/
if(rand() % 2 == 0)
num[i] = rand();
else
num[i] = (-1) * rand();
if(n < 100)
cout<<num[i]<<" ";
}
cout<<endl;
//蛮力法//
cout<<"\n蛮力法:"<<endl;
cout<"最大字段和:";
QueryPerformanceCounter(&begin);
{
rights += a[j];
if(rights > s2) s2 =rights;
}
sum = s1 + s2;//计算第3钟情况的最大子段和
if(sum < leftsum) sum = leftsum;//合并,在sum、leftsum、rightsum中取最大值
if(sum < rightsum) sum = rightsum;
const int n = 40;
LARGE_INTEGER begin,end,frequency;
QueryPerformanceFrequency(&frequency);
//生成随机序列
cout<<"生成随机序列:";
srand(time(0));
for(int i = 0; i < n; i++)
cout<<BF_Sum(num,n)<<endl;
QueryPerformanceCounter(&end);
cout<<"时间:"
<<(double)(end.QuadPart - begin.QuadPart) / frequency.QuadPart
<<"s"<<endl;
cout<<"\n分治法:"<<endl;
6.ThisSum = 0; /* ThisSum是从A[i]到A[j]的子列和*/
7.for( k = i; k <= j; k++ )
8.ThisSum += A[k];
9.if( ThisSum > MaxSum ) /*如果刚得到的这个子列和更大*/
10.MaxSum = ThisSum; /*则更新结果*/
<<"s"<<endl;
cout<<"\n动态规划法:"<<endl;
cout<"最大字段和:";
QueryPerformanceCounter(&begin);
cout<<DY_Sum(num,n)<<endl;
QueryPerformanceCounter(&end);
cout<<"时间:"
<<(double)(end.QuadPart - begin.QuadPart) / frequency.QuadPart
b[i] = b[i - 1] + a[i];
else
b[i] = a[i];
}
for(int j = 0; j < n; j++)
{
if(b[j] > sum)
sum = b[j];
}
delete []b;//释放内存
return sum;
}
int main()
{
int num[MAX];
int i;
MaxSum = ThisSum; /*则更新结果*/
} /* j循环结束*/
} /* i循环结束*/
return MaxSum;
}
最大子段和nlongn算法(分治)
int maxSum(int a[],int left, int right)
{
int sum = 0;
if(left == right)//如果序列长度为1,直接求解
#include<time.h>
#include<Windows.h>
using namespace std;
#define MAX 10000
int BF_Sum(int a[],int n)
{
int max=0;
int sum=Fra Baidu bibliotek;
int i,j;
for (i=0;i<n-1;i++)
{
sum=a[i];
最大子段和n3算法
1.int MaxSubseqSum1( int A[], int N )
2.{ int ThisSum, MaxSum = 0;
3.int i, j, k;
4.for( i = 0; i < N; i++ ) { /* i是子列左端位置*/
5.for( j = i; j < N; j++ ) { /* j是子列右端位置*/
{
if(b[i-1] > 0)
b[i] = b[i - 1] + a[i];
else
b[i] = a[i];
}
for(int j = 0; j < n; j++)
{
if(b[j] > sum)
sum = b[j];
}
delete []b;//释放内存
return sum;
}
完整测试程序:
#include<iostream>
{
rights += a[j];
if(rights > s2) s2 =rights;
}
sum = s1 + s2;//计算第3钟情况的最大子段和
if(sum < leftsum) sum = leftsum;//合并,在sum、leftsum、rightsum中取最大值
if(sum < rightsum) sum = rightsum;
cout<"最大字段和:";
QueryPerformanceCounter(&begin);
cout<<maxSum1(num,0,n)<<endl;
QueryPerformanceCounter(&end);
cout<<"时间:"
<<(double)(end.QuadPart - begin.QuadPart) / frequency.QuadPart
{
if(a[left] > 0) sum = a[left];
else sum = 0;
}
else
{
int center = (left + right) / 2;//划分
int leftsum = maxSum(a,left,center);//对应情况1,递归求解
int rightsum = maxSum(a, center + 1, right);//对应情况2,递归求解
{
if(a[left] > 0) sum = a[left];
else sum = 0;
}
else
{
int center = (left + right) / 2;//划分
int leftsum = maxSum1(a,left,center);//对应情况1,递归求解
int rightsum = maxSum1(a, center + 1, right);//对应情况2,递归求解
11.} /* j循环结束*/
12.} /* i循环结束*/
13.return MaxSum;
14.}
最大子段和n2算法
int MaxSubseqSum2( int A[], int N )
{ int ThisSum, MaxSum = 0;
int i, j;
for( i = 0; i < N; i++ ) { /* i是子列左端位置*/
int s1 = 0;
int lefts = 0;
for(int i = center; i >= left; i--)//求解s1
{
lefts += a[i];
if(lefts > s1) s1 = lefts;//左边最大值放在s1
}
int s2 = 0;
int rights = 0;
for(int j = center + 1; j <= right; j++)//求解s2
int s1 = 0;
int lefts = 0;
for(int i = center; i >= left; i--)//求解s1
{
lefts += a[i];
if(lefts > s1) s1 = lefts;//左边最大值放在s1
}
int s2 = 0;
int rights = 0;
for(int j = center + 1; j <= right; j++)//求解s2
<<"s"<<endl;
system("pause");
return 0;
}
}
return sum;
}
最大子段和动态规划算法
int DY_Sum(int a[],int n)
{
int sum = 0;
int *b = (int *) malloc(n * sizeof(int));//动态为数组分配空间
b[0] = a[0];
for(int i = 1; i < n; i++)
for(j=i+1;j<n;j++)
{
if(sum>=max)
{
max=sum;
}
sum+=a[j];
}
}
return max;
}
int maxSum1(int a[],int left, int right)
{
int sum = 0;
if(left == right)//如果序列长度为1,直接求解
}
return sum;
}
int DY_Sum(int a[],int n)
{
int sum = 0;
int *b = (int *) malloc(n * sizeof(int));//动态为数组分配空间
b[0] = a[0];
for(int i = 1; i < n; i++)
{
if(b[i-1] > 0)
ThisSum = 0; /* ThisSum是从A[i]到A[j]的子列和*/
for( j = i; j < N; j++ ) { /* j是子列右端位置*/
ThisSum += A[j]; /*对于相同的i,不同的j,只要在j-1次循环的基础上累加1项即可*/
if( ThisSum > MaxSum ) /*如果刚得到的这个子列和更大*/