码迷,mamicode.com
首页 > 编程语言 > 详细

返回一个整数数组中最大子数组的值(程序能处理1000个元素)

时间:2018-10-14 19:20:49      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:com   题目   mes   return   技术   cout   元素   +=   using   

 课堂练习:
题目:要求返回一个整数数组中最大子数组的值
要求:程序必须能处理1000个元素
          每个元素是int32类型的
 
设计思路:

将数组的大小定义为1000,每个元素定义为int32类型,取数值时对数成2的32次方,这样数值可以越界。
 
程序:
 
 
#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();
     }
   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;
 }
运行结果:
技术分享图片

合影:

技术分享图片

 

返回一个整数数组中最大子数组的值(程序能处理1000个元素)

标签:com   题目   mes   return   技术   cout   元素   +=   using   

原文地址:https://www.cnblogs.com/gaoemm/p/9787109.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!