Example #1
0
int main()
{
	FILE *fp = NULL;
	int a = 0, b = 0, t, i = 0;
	time_t startTime, finishTime;

	if ((fp = fopen("testdata.txt", "r")) == NULL) return 0;
	fscanf(fp, "%d", &t);
	if (t > MAX) t = MAX;

	while (t--)
	{
		fscanf(fp, "%d", &arr[i]);
		i++;
	}
	fclose(fp);

	startTime = clock();
	printf("%d\n",maxSubsequenceSum(0, MAX));
	finishTime = clock();

	fp = fopen("result.txt", "a");
	if (fp == NULL)
	{
		fp = fopen("result.txt", "w");
		fprintf(fp, "counter : %lld ,data : %ld , runtime %ld ms\n", count, MAX, finishTime-startTime);
		fclose(fp);
	}

	fprintf(fp, "counter : %lld ,data : %ld , runtime %ld ms\n", count, MAX, finishTime - startTime);
	fclose(fp);
	return 0;
}
int maxSubsequenceSum(int A[], int left, int right)
{
/* maxLeftSum左半部分最大和,maxLeftBorderSum左半边界(带左半最右边元素如A[3])最大和,leftBorderSum左半边界和 */
    int maxLeftSum, maxRightSum;
    int maxLeftBorderSum, maxRightBorderSum;
    int leftBorderSum ,rightBorderSum;
    int center, i;
    
    if(left == right)
        if(A[left] > 0)
            return A[left];
        else
	    return 0;

    center = (left + right) / 2; /* 向下取整 */
    maxLeftSum = maxSubsequenceSum(A, left, center);    /* 左半最大子序列和 */
    maxRightSum = maxSubsequenceSum(A, center + 1, right);

    maxLeftBorderSum = 0;    /* 左半边界最大和 */
    leftBorderSum = 0;    /* 左半边界和 */
    for(i = center; i >= left; i--) 
    {    /* 这里貌似用了方法二种的遍历方式,起点是center, end是左半首元素 */
        leftBorderSum += A[i];
        if(leftBorderSum > maxLeftBorderSum)
            maxLeftBorderSum = leftBorderSum;
    }
    
	/* 同理 */
    maxRightBorderSum = 0;
    rightBorderSum = 0;
    for(i = center + 1; i <= right; i++) 
    {  /* 这里貌似用了方法二种的遍历方式,起点是center + 1, end是右半末元素 */
        rightBorderSum += A[i];
        if(rightBorderSum > maxRightBorderSum)
            maxRightBorderSum = rightBorderSum;
    }
	/* 求左半最大,右半最大,(左半边界最大+右半边界最大)的最大者 */
    return max3(maxLeftSum, maxRightSum, maxLeftBorderSum + maxRightBorderSum);  
}
Example #3
0
int maxSubsequenceSum(int start, int end){
	if (start == end){
		return arr[start] > 0 ? arr[start] : 0;
	}

	int center = (start + end) / 2;
	int maxLeftSum = maxSubsequenceSum(start, center);
	int maxRightSum = maxSubsequenceSum(center + 1, end);

	int maxLeftCenterSum = 0;
	int maxRightCenterSum = 0;
	int leftCenterSum = 0;
	int rightCenterSum = 0;

	for (int i = center; i >= start; --i)
	{
		leftCenterSum += arr[i]; // basic operation
		count++;
		if (leftCenterSum > maxLeftCenterSum)
			maxLeftCenterSum = leftCenterSum;
	}

	for (int i = center + 1; i <= end; ++i)
	{
		rightCenterSum += arr[i]; // basic operation
		count++;
		if (rightCenterSum > maxRightCenterSum)
			maxRightCenterSum = rightCenterSum;
	}

	int max = maxLeftSum;
	if (maxRightSum>max)
		max = maxRightSum;
	if (maxLeftCenterSum + maxRightCenterSum > max)
		max = maxLeftCenterSum + maxRightCenterSum;

	return max;
}
int main(int argc, char *argv[]){

    int seq[] = {5, 6, 4, -5, 7, -3, 12, -5, 8, 5}, LEN = 10;

    printf("MAX SUM=%d\n", maxSubsequenceSum(seq, LEN));
    printf("MAX SUM=%d\n", maxSubsequenceSum2(seq, LEN));
    printf("MAX SUM=%d\n", maxSubsequenceSum3(seq, LEN));
    printf("MAX SUM=%d\n", maxSubsequenceSum4(seq, LEN));
    printf("MIN SUM=%d\n", minSubsequenceSum(seq, LEN));
    printf("MIN PSUM=%d\n", minPosSubsequenceSum(seq, LEN));
    printf("MIN PSUM=%d\n", minPosSubsequenceSum2(seq, LEN));
    printf("MAX MUL=%d\n", maxSubsequenceMul(seq, LEN));
    printf("MAX MUL=%d\n", maxSubsequenceMul2(seq, LEN));

    return 0 ;
}
Example #5
0
int main()
{
	FILE *fp =NULL;
	time_t start_time, finish_time, h, m, s;
	int a = 0, b = 0, t, i = 0;
	
	
	if ((fp = fopen("testdata.txt", "r")) == NULL) return 0;
	fscanf(fp, "%d", &t);
	if (t > MAX) t = MAX;
	
	while (t--)
	{
		fscanf(fp, "%d", &arr[i]);
		i++;
	}
	fclose(fp);

	

	time(&start_time);
	printf("MAX VALUE : %d\n",maxSubsequenceSum(MAX, &a, &b));
	time(&finish_time);

	h = (finish_time - start_time) / 3600;
	m = ((finish_time - start_time) - (h * 3600)) / 60;
	s = (finish_time - start_time) - ((h * 3600) + (m * 60));

	fp = fopen("result.txt", "a");
	if (fp == NULL)
	{
		fp = fopen("result.txt", "w");
		fprintf(fp, "data : %ld , runtime %lldh:%lldm:%llds\n",MAX, h, m, s);
		fclose(fp);
	}
	
	fprintf(fp, "data : %ld , runtime %lldh:%lldm:%llds\n",MAX, h, m, s);
	fclose(fp);
	
	return 0;
}
main()
{
    int N = 8, maxSum = 0;    
    char p[256];
    char *str = p;
    ElementType A[] = {4, -3, 5, -2, -1, 2, 6, -2};

    printf("\t ========== [third method-devide and conquer] test for maximum sum of subsequence ==========\n\n");
    str = "initial array A: ";
    printf("%50s",str);
    printArray(A, N);    

    str = "after computing maximum sum of subsequence: ";
    printf("%50s", str);
    maxSum = maxSubsequenceSum(A, 0, N-1);
    printArray(A, N);

    str = "maximum sum of array subsequence is: ";
    printf("%50s", str);
    printf("%4d\n\n", maxSum);

    return 0;
}