示例#1
0
        /**
         * Recursive maximum contiguous subsequence sum algorithm.
         * Finds maximum sum in subarray spanning a[left..right].
         * Does not attempt to maintain actual best sequence.
         */
        int maxSumRec( const vector<int> & a, int left, int right )
        {
/* 1*/      if( left == right )  // Base case
/* 2*/          if( a[ left ] > 0 )
/* 3*/              return a[ left ];
                else
/* 4*/              return 0;

/* 5*/      int center = ( left + right ) / 2;
/* 6*/      int maxLeftSum  = maxSumRec( a, left, center );
/* 7*/      int maxRightSum = maxSumRec( a, center + 1, right );

/* 8*/      int maxLeftBorderSum = 0, leftBorderSum = 0;
/* 9*/      for( int i = center; i >= left; i-- )
            {
/*10*/          leftBorderSum += a[ i ];
/*11*/          if( leftBorderSum > maxLeftBorderSum )
/*12*/              maxLeftBorderSum = leftBorderSum;
            }

/*13*/      int maxRightBorderSum = 0, rightBorderSum = 0;
/*14*/      for( int j = center + 1; j <= right; j++ )
            {
/*15*/          rightBorderSum += a[ j ];
/*16*/          if( rightBorderSum > maxRightBorderSum )
/*17*/              maxRightBorderSum = rightBorderSum;
            }

/*18*/      return max3( maxLeftSum, maxRightSum,
/*19*/                   maxLeftBorderSum + maxRightBorderSum );
        }
示例#2
0
int main(){
	double table[ROW_CAP][COL_COUNT];
	FILE* inFile;
	int nRow;
	double maxSum;
	int lUY, lUX, rDY, rDX;

	inFile=fopen("Table1.txt", "r");

	getArray(inFile, table, &nRow);

	maxSum=maxSumConstPoint(table, nRow, 2, 3, &rDY, &rDX);
	printf("MaxSum Rectangular starting from origin is %.2lf. Its right down coordinate (y,x) is %d, %d\n", maxSum, rDY, rDX);



	maxSum=maxSumRec(table, nRow, &lUY, &lUX, &rDY, &rDX);
	printf("MaxSum Rectangular is %.2lf. Its left upper coordinate (y,x) is %d, %d, right down coordinate is %d, %d\n", maxSum, lUY, lUX, rDY, rDX);

	fclose(inFile);
	return 0;
}
示例#3
0
 /**
  * Driver for divide-and-conquer maximum contiguous
  * subsequence sum algorithm.
  */
 int maxSubSum3( const vector<int> & a )
 {
     return maxSumRec( a, 0, a.size( ) - 1 );
 }