选出了不连续的元素之和
一开始思路错误了,这样编写程序只能选出不连续的子列和,但题目一般是要求连续的
//the maximum of subsequences
//Divide-and-Conquer
#include<stdio.h>
#include<stdlib.h>
int* GeneArray(int n);
int MaxSubSeq(int* a, int n);
int Sort(int*a, int left, int right);
int Merge(int max1, int max2);
int main()
{
int n; //the size of the array
int maxsum;//the maximum of the submatrix
printf("Please input the size of the array to be sorted : ");
scanf("%d",&n);
int* array = GeneArray(n);
int i;
for(i = 0; i < n; i++)
{
printf("%d ", array[i]);
}
maxsum = MaxSubSeq(array, n);
printf("\nthe maximum of subsequence is %d", maxsum);
return 0;
}
int MaxSubSeq(int*a, int n)
{
int max;
max = Sort(a, 0, n - 1);
return max;
}
int Sort(int*a, int left, int right)
{
int mid = left + (right - left)/2;
if(left == right)
{
return a[left];
}
else
{
int max1 = Sort(a, left, mid);//return max1
int max2 = Sort(a, mid + 1, right);//return max2
return Merge(max1, max2);
}
}
int Merge(int max1, int max2)
{
int sum = max1 + max2;
int m0 = max1 > max2 ? max1 : max2;
int max = sum > m0 ? sum : m0;
return max;
}
int* GeneArray(int n)
{
srand((unsigned int)time(0));//initialize the seed
int i;
int*array = (int*)malloc(sizeof(int)*n);
for(i = 0; i < n; i++)
{
array[i] = rand()%200 - 100;//to generate random number from -100 to 100
}
return array;
}
原来是把数列分成了三部分,left mid right, 现在只要每一小块都把经过mid 的左右联通部分考虑进去就可以了
C源码
//the maximum of subsequences
//Divide-and-Conquer
#include<stdio.h>
#include<stdlib.h>
int Max(int a, int b);
int* GeneArray(int n);
int SubMax(int*a, int left, int right);
int main()
{
int n; //the size of the array
int maxsum;//the maximum of the submatrix
printf("Please input the size of the array to be sorted : ");
scanf("%d",&n);
int* array = GeneArray(n);
int i;
for(i = 0; i < n; i++)
{
printf("%d ", array[i]);
}
maxsum = SubMax(array, 0, n - 1);
printf("\nthe maximum of subsequence is %d", maxsum);
return 0;
}
int SubMax(int*a, int left, int right)
{
if(left == right)//when only one element left
{
return a[left];//return the one
}
int mid = left + (right - left)/2;
int leftmax = 0;//merely the left part
int rightmax = 0;//merely the right part
int bordermax = 0;//the maximum that crossed the mid
int borderleft = 0;//the maximum of the left part started from the mid
int borderright = 0;//the maximum of the right part started from the mid + 1
int leftsum = 0;//the sum of partial left part started from the mid
int rightsum = 0;//the sum of partial right part started from the mid + 1
int max = 0;//the maximum sum of the subsequence
int i;
//dividing
leftmax = SubMax(a, left, mid);
rightmax = SubMax(a, mid + 1, right);
//consider the left part
for(i = mid; i >= left; i--)
{
leftsum += a[i];
if(leftsum > borderleft)
borderleft = leftsum;
}
//consider the right part
for(i = mid + 1; i <= right; i++)
{
rightsum += a[i];
if(rightsum > borderright)
borderright = rightsum;
}
bordermax = borderleft + borderright;
max = Max(bordermax, Max(rightmax, leftmax));//the max depends on the left, the right and the the one crossed the border
return max;
}
int* GeneArray(int n)
{
srand((unsigned int)time(0));//initialize the seed
int i;
int*array = (int*)malloc(sizeof(int)*n);
for(i = 0; i < n; i++)
{
array[i] = rand()%200 - 100;//to generate random number from -100 to 100
}
return array;
}
int Max(int a, int b)
{
if(a >= b)
return a;
else if(b > a)
return b;
}