数组的最大子序列和

来源:互联网 发布:wiki全球软件发展趋势 编辑:程序博客网 时间:2024/05/17 07:13
求数组(元素可为正数、负数、0)的最大子序列和

int maxSum(int a[], int n){      int max = a[0];      int total = a[0];      for (int i = 1; i < n; i++)      {          if (total <= 0)          {              total = a[i];          }          else          {              total += a[i];          }          if (total > max)          {              max = total;          }      }      return max;}