zoukankan      html  css  js  c++  java
  • 数组中球子数组和最大的问题(结对开发)

     1 //作者:王炳午  董龙洋  日期:2015.3.17
     2 #include<iostream.h>
     3 #include<stdlib.h>
     4 #include<time.h>
     5 int main()
     6 {
     7     cout<<"---------------------求数组中子数组和的最大值的小程序----------------------"<<endl;
     8     cout<<endl;
     9     int a[10];
    10     int i;
    11     srand( (unsigned)time( NULL ) );//随机数种子为当前计算机时间。
    12     cout<<"得到的一组随机整数(10个数)如下:"<<endl;
    13     for(i=0;i<10;i++)
    14     {
    15         a[i] = rand()%21-10; //随机生成-10 到 10的整数
    16     }
    17     for(i=0;i<10;i++)
    18     {
    19         cout<<a[i]<<"      ";
    20     }
    21     cout<<endl;
    22     int sum=0;  
    23     int GreateSum=0;  
    24     for ( i=0;i<10;i++)  
    25     {  
    26         sum+=a[i];  
    27         if (sum<0)  
    28         {  
    29             sum=0;  
    30         }  
    31         if (sum>GreateSum)  
    32         {  
    33             GreateSum=sum;  
    34         }  
    35     }  
    36     if (GreateSum==0)  
    37     {  
    38         for (int i=0;i<10;i++)  
    39         {  
    40             if (GreateSum==0)  
    41             {  
    42                 GreateSum=a[i];  
    43             }  
    44             else  
    45             if (GreateSum<a[i])  
    46             {  
    47                 GreateSum=a[i];  
    48             }  
    49         }  
    50     }  
    51     cout<<"最大值sum:"<<GreateSum<<endl;  
    52     system("pause");  
    53     return 0;  
    54 }  

    感想:两人一起去构思一起去想算法,本题最难的是算法的构思,开始时我和我的小伙伴都没有注意时间复杂度只能为n,于是我充当领航员的角色,合作下主要是由他书写代码和细节类的书写,完成了一个代码,可是后来发现时间复杂度为n*n。接着我两就想法简化时间复杂度,利用DP算法构思出了程序,在原来的一些基础上,共同完成了本次任务,我觉得两人结对,会使发现错误的几率大大增加,特别是一些算法上的问题,对代码的完成速度和质量都有很大的好处。以下是我和小伙伴的合照(左边是我,右边是我的小伙伴)

  • 相关阅读:
    springboot+jsp 遇到的坑
    异步复位同步释放
    DDR工作原理(转载)
    FPGA基础 之逻辑单元
    二进制转BCD
    bcd转二进制
    FPGA学习笔记之IIC—EEPROM写和读
    FPGA学习笔记之mif文件生成方法总结
    FPGA_实验小项目:四位运算小计算器
    小小计算器之消零显示模块数码管消零
  • 原文地址:https://www.cnblogs.com/mtant/p/4346707.html
Copyright © 2011-2022 走看看