实验一实验报告

设计思想:实验一的要求是在未知数组中求得最大的连续子数组的和,我的设计理念是判断首字符的正负,如果其为正,那么就遍历其后面的数组,直到其与该数组的和为负数时停止,这时对在变为负数之前的数组进行保存,同时以另一个定义的数组再次进行如上操作,知道遍历结束,最后比较这些数组的大小,取最大值为结果。

源程序代码:

include<stdio.h>

int main()

{

int s,b;

int a[s];

printf("%d/n",please input the size of the array);

scanf("%d/n",&s);

printf("%d/n",Please input the array);

scanf("%d/n",&a[s]);

b=FindGreatestSumOfSubArray(a[s]);

printf("%d/n",b);

return 0;

}

 

int FindGreatestSumOfSubArray(vector<int> array)

{     

       if(array.size() == 0)             

              return -1;     

       int sum = array[0];

       int max = array[0];

       for (size_t size = 1; size < array.size(); ++size) 

       {            

              if(sum <= 0)               

                     sum = array[size];       

              else              

                     sum += array[size];            

              if (sum > max)                   

                     max = sum;  

}

       return max;

}

运行结果截图:

项目计划日志

时间记录日志:

缺陷记录日志:

猜你喜欢

转载自www.cnblogs.com/GXCblog/p/9751256.html