Advertisement
lily09290110

連續數字最大和

Jan 24th, 2015
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.46 KB | None | 0 0
  1. #include<stdio.h>
  2. int main()
  3. {
  4.   int sum[105],max[105],n1,n2,temp,count=0,i;
  5.   for(scanf("%d",&n1);n1>=1;n1--)
  6.   {
  7.      for(scanf("%d",&n2);n2>=1;n2--)scanf("%d",&sum[count++]);
  8.      max[0]=sum[0];
  9.      temp=sum[0];
  10.      for(i=1;i<count;i++)
  11.      {
  12.          (temp<0)&&(temp=0);
  13.          temp+=sum[i];
  14.          sum[i]=temp;
  15.          (max[i-1]>=sum[i])?max[i]=max[i-1]:(max[i]=sum[i]);
  16.      }
  17.      printf("%d\n",max[i-1]);
  18.      count=0;
  19.   }
  20.   return 0;
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement