最大子序列和的四种算法之讲解

本文详细介绍了求解最大子序列和的四种算法,从暴力求解的0(N^3)时间复杂度,到二重循环的0(N^2)方法,再到利用分治思想降低时间复杂度到0(N log N),最后是仅需一次扫描的0(N)高效算法。通过这些方法的讲解,帮助读者深入理解如何优化算法效率。
摘要由CSDN通过智能技术生成


----------------------------------------------------------------------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------------------------------------------------------------------------



方法一:

  • 暴力求解法,即使用三重循环进行遍历,其时间复杂度为0(N^3)
代码如下:
/*
--------------------------------------------------
function: to calculate the maximum sequence sum of
		  the given sequence; if all are negative,
		  to make the sum equal 0
example:
	input: 6
		   -7 6 -3 4 -2 1
	output: 7

author: sunny
date: 2017.03.05
platform: ubuntu 14.04LTS
complier: gcc 
-------------------------------------------------
 */

#include <stdio.h>


int GetNumCount(void);  //to get the count of the number
void GetSeqNum(int [], int);
void ClearIOBuffer(void);  //to clear the input buffer
int GetMaxSeqSum(int [], int);

int main(void)
{
	int numCount, MaxSeqSum;
	int numArray[numCount];

	numCount = GetNumCount();
	GetSeqNum(numArray, numCount);	
	MaxSeqSum = GetMaxSeqSum(numArray, numCount);
	printf("MaxSeqSum = %d\n", MaxSeqSum);

	return 0;
}

int GetNumCount(void) {
	
	int count;
	printf("Enter the count of number:");
	scanf("%d", &count);
	ClearIOBuffer();

	return count;
}

void ClearIOBuffer(void) {
	
	while(getchar() !=
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值