zoukankan      html  css  js  c++  java
  • 求最大数组和

     设计思想:定义数组为1000个,选数时进行*42946578956,使数组内数字越界
     
    #include <iostream>
     #include<stdlib.h>
     #include<time.h>
     using namespace std;

     int main()
     {
         int i;
         int a[10000];
         int max = 0;
         int b = 0;
     
         srand(time(NULL));
         cout<<"数组为:"<<endl;
         for (i = 0; i<10000; i++)
         {
             a[i] = rand() ;
         }
         for (i = 0; i<10000; i++)
         {
             cout << a[i] << ' ';
         }
         cout << endl;
     
         for (i = 0; i < 10000; i++)
         {
             b += a[i];
             if (b < 0)
                 b = 0;
             if (b > max)
                 max = b;
         }
         if (max == 0)
         {
             max = a[0];
             for (i = 0; i < 10000; i++)
             {
                 if (max < a[i])
                 {
                     max = a[i];
                 }
             }    
         }
         cout <<"最大子数组为:"<< max << endl;
         system("pause");
         return 0;
     }

     无溢出

  • 相关阅读:
    mvc 在ii7上发布时遇到的问题只解决
    @Html.Raw 显示Html的标签
    补发————grid布局
    补发————DOM与BOM
    web实验博客3.css-position
    web专业课学习及往后方向发展
    bom&dom
    网格代码
    简单用户注册表单
    自我介绍
  • 原文地址:https://www.cnblogs.com/mengweile/p/9787279.html
Copyright © 2011-2022 走看看