Position: All -> Works -> EOJ 1109 Max Sum | 08-03-28 22:03:40
Font-Size:
Large | Middle | Small

Description

¸øÄãÒ»¸öÐòÁÐa[1],a[2],a[3]......a[n], ÄãµÄÈÎÎñÊÇÈ¥¼ÆËãÕâ¸öÐòÁÐÖкÍ×î´óµÄÒ»¸ö×ÓÐòÁÐ. ÀýÈç, ¸øÄã¸öÐòÁÐ(6,-1,5,4,-7), ºÍ×î´óµÄÒ»¸ö×ÓÐòÁÐÊÇ 6 + (-1) + 5 + 4 = 14.
±¾ÌâÄ¿ÊÇÇó×î´ó£¨Á¬Ðø£©×ÓÐòÁеĺÍ

Input

ÔÚµÚÒ»ÐÐÊäÈëÒ»¸öÊý T(1<=T<=20) Òâ˼ÊÇÊäÈëÐòÁеĸöÊý. È»ºóºóÃæTÐÐ, ÿÐпªÊ¼Ò»¸öÊýÊÇ N(1<=N<=100)£¬Õâ¸öÐòÁÐÓÐN¸öÊý, È»ºó½ÓÏÂÀ´ÓÐN¸öÊý(ÿ¸öÊýµÄ·¶Î§ÊÇ -1000µ½ 1000).

Output

¶Ôÿ¸ö²âÊÔ£¬Êä³ö×ÔÐòÁк͵Ä×î´óÖµ¡£

Sample Input

2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5

Sample Output

14
7

Source

ECNUËã·¨×÷Òµ

C++´úÂë
  1. #include<stdio.h>  
  2. int main() {  
  3.     int i,t,n,a[101];  
  4.     scanf("%d",&t);  
  5.     while(t--) {  
  6.         scanf("%d",&n);  
  7.         int sum = 0,ans = -99999999;;  
  8.         for(i = 0;i < n;i++) {  
  9.             scanf("%d",&a[i]);  
  10.             sum += a[i];  
  11.             if(sum > ans)  
  12.                 ans = sum;  
  13.             if(sum < 0)  
  14.                 sum = 0;  
  15.         }  
  16.         printf("%d\n",ans);  
  17.     }  
  18. }  

Share to:

Comments:

ghdjApr 26th, 2009 at 2:51 am
#1
¾­µä

Add Comments: