软工课后作业4:返回一个整数数组中最大子数组的和2

要求程序必须能处理1000 个元素;

每个元素是int32 类型的;

输入一个整形数组,数组里有正数也有负数。

数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和。

求所有子数组的和的最大值。——摘要

编程代码

#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])

{

 return 0;

}

 #include <iostream>

 #include<stdlib.h>

 #include<time.h>

 using namespace std;

 int main()

 {

     int i;

    int a[1000];

     int max = 0;

     int b = 0;

    srand(time(NULL));

     cout<<"数组为:"<<endl;

    for (i = 0; i<1000; i++)

     {

         a[i] = rand()*4294967296;

     }

     for (i = 0; i<1000; i++)

     {

         cout << a[i] << '\t';

     }

     cout << endl;

     for (i = 0; i < 1000; i++)

     {

         b += a[i];

         if (b < 0)

             b = 0;

         if (b > max)

             max = b;

     }

     if (max == 0)

     {

         max = a[0];

         for (i = 0; i < 1000; i++)

         {

             if (max < a[i])

             {

                max = a[i];

             }

         }    

     }

     cout <<"最大子数组为:"<< max << endl;

     system("pause");

     return 0;

 }

posted on 2019-01-10 02:33  失望来于期望  阅读(116)  评论(0编辑  收藏  举报

导航