NYOJ+dp最大字串和+注意初始化赋值那,

来源:互联网 发布:淘宝卖家如何寄快递 编辑:程序博客网 时间:2024/05/22 19:06
点击打开链接
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<string>#include<cstring>#include<iostream>#include<algorithm>#include<math.h>#include<cstdio>using namespace std;int dp[1000010];int main(){    int N=0,n=0,i=0;    long long maxn=0,temp=0;    scanf("%d",&N);    while(N--){        scanf("%d",&n);        maxn=0,temp=0;        for(i=0;i<n;i++){            scanf("%d",&dp[i]);        }        temp=dp[0];maxn=dp[0];///maxn与temp的赋值要注意。        for(i=1;i<n;i++){            if(temp<0) temp=dp[i];            else temp+=dp[i];            if(temp>maxn) maxn=temp;        }        printf("%lld\n",maxn);    }    return 0;}

0 0
原创粉丝点击