程序员算法之求最大子数组的和

View Code
 1 #include <stdio.h>

 2 

 3 int main()

 4 {

 5     int subsum = 0;

 6     int great = 0; 

 7     int a[8] = {1,-2,3,10,-4,7,2,-5};

 8     for(int i = 0;i < 8;i++)

 9     {

10         subsum = subsum + a[i];

11         if(subsum < 0) subsum = 0;

12         if(subsum > great)

13         {

14             great = subsum;

15         }

16     }

17     

18     printf("%d\n",great);

19     return 0;

20 }

 

你可能感兴趣的:(程序员)