01-复杂度1 最大子列和问题

1.最大子列和问题

在这里插入图片描述
在这里插入图片描述

1. 代码如下:在线处理——O(N)

#include <stdio.h>

int MaxSubseqSum4(int A[], int N){
    int ThisSum, MaxSum;
    int i;
    ThisSum = MaxSum = 0;
    for(i=0; i<N; i++){
        ThisSum += A[i];
        if(ThisSum > MaxSum)
            MaxSum = ThisSum;
        else if(ThisSum < 0)
            ThisSum = 0;
    }
    return MaxSum;
}

int main(){
    int a, N;
    scanf("%d\n", &N);
    int List[N];
    for(int i=0; i<N; i++){
        scanf("%d", &a);
        List[i] = a;
    }
    int max = MaxSubseqSum4(List, N);
    printf("%d\n", max);
    
    return 0;
}

2. 其他解法

2.1 O(N^3)

#include <stdio.h>

int MaxSubseqSum1(int A[], int N){
    int ThisSum, MaxSum = 0;
    int i, j, k;
    for(i=0; i<N; i++){
        for(j=i; j<N; j++){
            ThisSum = 0;
            for(k=i; k<=j; k++)
                ThisSum += A[k];
            if(ThisSum > MaxSum)
                MaxSum = ThisSum;            
        }
    }
    return MaxSum;
}

int main(){
    int a, N;
    scanf("%d\n", &N);
    int List[N];
    for(int i=0; i<N; i++){
        scanf("%d", &a);
        List[i] = a;
    }
    int max = MaxSubseqSum1(List, N);
    printf("%d\n", max);
    
    return 0;
}

2.2 分而治之

#include <stdio.h>

int Max3(int a, int b, int c){
    return a>b ? a>c ? a : c : b>c ? b :c;
}

int DivideAndConquer(int List[], int left, int right){
    int MaxLeftSum, MaxRightSum;    /*存放左右子问题的解*/
    int MaxLeftBorderSum, MaxRightBorderSum;    /*存放跨分界线的结果*/
    
    int LeftBorderSum, RightBorderSum;
    int center, i;
    
    if(left == right){
        if(List[left]>0) 
            return List[left];
        else
            return 0;
    }
    
     /*下面为"分"*/
     center = (left + right) / 2;
     
     /*递归求得两边的最大和*/
     MaxLeftSum = DivideAndConquer(List, left, center);
     MaxRightSum = DivideAndConquer(List, center+1, right);
     
     /*求跨分界线的最大子列和*/
     MaxLeftBorderSum = 0;
     LeftBorderSum = 0;
     /*从中线向左扫描*/
     for(i=center; i>=left; i--){
         LeftBorderSum += List[i];
         if(LeftBorderSum > MaxLeftBorderSum)
            MaxLeftBorderSum = LeftBorderSum;
     }
     
     MaxRightBorderSum = 0;
     RightBorderSum = 0;
     /*从中线向右扫描*/
     for(i=center+1; i<=right; i++){
         RightBorderSum += List[i];
         if(RightBorderSum > MaxRightBorderSum)
            MaxRightBorderSum = RightBorderSum;
     }
     
     /*下面返回"治"的结果*/
     return Max3(MaxLeftSum, MaxRightSum, MaxLeftBorderSum+MaxRightBorderSum);
}

int MaxSubseqSum3(int List[], int N){
    return DivideAndConquer(List, 0, N-1);
}

int main(){
    int a, N;
    scanf("%d\n", &N);
    int List[N];
    for(int i=0; i<N; i++){
        scanf("%d", &a);
        List[i] = a;
    }
    int max = MaxSubseqSum3(List, N);
    printf("%d\n", max);
    
    return 0;   
}

运行结果如下:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值