zoukankan      html  css  js  c++  java
  • 返回整数数组最大子数组

    应王老师要求,和李子木同学共同完成编程任务。

           要求程序必须能处理1000 个元素,每个元素是int32 类型的;输入一个整形数组,数组里有正数也有负数。

      数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和。
      求所有子数组的和的最大值。要求时间复杂度为O(n)。 
    #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()*4294967296 ;
        }
        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;
    }
    截图:
     
    感想:
    有的时候程序运行结果会是都是0,这好像是因为数据过多导致程序出错,数据长度减少就可以解决了。程序比较难,需要多加考虑。
    附上和李子木同学的合照。。
     
     
  • 相关阅读:
    基于角色的权限控制
    C#中实现拖动无边框窗体Form
    C#加密方法汇总
    监控 SQL Server (2005/2008) 的运行状况来自微软TetchNet
    经典SQL语句集锦【转】
    Asp.net+Xml+js实现无线级下拉菜单
    CuteEditor5.0的安装及它与Ajax.net配合无刷新操作数据库!
    sql 提取数字、字母、汉字
    c# 判断远程文件是否存在
    c#采集网页用得几个函数
  • 原文地址:https://www.cnblogs.com/miniarcher/p/9787106.html
Copyright © 2011-2022 走看看