PAT-最大子列和問題

來源:互聯網
上載者:User

標籤:style   blog   io   ar   color   os   sp   for   strong   

01-1. 最大子列和問題(20)時間限制10000 ms
記憶體限制65536 kB
代碼長度限制8000 B
判題程式Standard

給定K個整數組成的序列{ N1, N2, ..., NK },“連續子列”被定義為{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j <= K。“最大子列和”則被定義為所有連續子列元素的和中最大者。例如給定序列{ -2, 11, -4, 13, -5, -2 },其連續子列{ 11, -4, 13 }有最大的和20。現要求你編寫程式,計算給定整數序列的最大子列和。

輸入格式:

輸入第1行給出正整數 K (<= 100000);第2行給出K個整數,其間以空格分隔。

輸出格式:

在一行中輸出最大子列和。如果序列中所有整數皆為負數,則輸出0。

輸入範例:
6-2 11 -4 13 -5 -2
輸出範例:
20
/*************************************************************************> File Name: 最大子列和問題.c> Author: ttop5> Blog: www.ttop5.net> Mail: [email protected]> Created Time: 2014年12月10日 星期三 09時16分13秒 ************************************************************************/#include<stdio.h>#define MAXN 100000int main(){    int i, N, a[MAXN];    int ThisSum, MaxSum;    ThisSum = MaxSum = 0;    scanf("%d", &N);    for(i=0; i<N; i++)    {        scanf("%d", &a[i]);    }    for(i=0; i<N; i++)    {        ThisSum += a[i];        if(ThisSum > MaxSum)        {            MaxSum = ThisSum;        }        else if(ThisSum < 0)        {            ThisSum = 0;        }    }    printf("%d\n", MaxSum);    return 0;}


PAT-最大子列和問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.