zoukankan      html  css  js  c++  java
  • 结对开发4_循环数组的最大值

    一、思路

        考虑到数组需要首尾相连,我们采取把数组的长度增加两倍,并把复制的数组写在在最后一个数的后面,

      这样在循环的过程中并不需要采用新的算法,只要把原来的代码稍微修改一下,加一个判断使得组成最大数组的

      长度不超过原数组的长度即可。

    二、源代码

      

    /*======================================================================
                # Author: TianYongTao && ZhangYaPeng
                # E-Mail: 714251829@qq.com
                # Last modified: 2015-03-28 20:48
                # Filename: Demo.cpp
                # Description: 计算一维回环数组中的最大子数组并将其输出
    ======================================================================*/
    # include "stdafx.h"
    # include <iostream>
    using namespace std;
    # define LENGTH 5
    
    int MaxSubArr(int * arr,int & start,int & count)            //start保存最大子数组起始点  count保存最大子数组长度
    {
        int max = 0;
        int max1 = arr[0];                                //max1保存的是全部为负数的数组中的最大值
        int temp = 0;
        bool flag=false; 
        for(int i=0;i<2*LENGTH;i++)                //判断数组元素是否全部小于零
        {
            if(arr[i]>0)
            {
                flag=true;
            }
            if(arr[i]>=max1)
            {
                max1=arr[i];
                start = i;
            }
        }
        if(!flag)
        {
            count=1;
            return max1;                                    //返回全部是负数的数组中最大的那个负数
        }
        start=0;
        for(int i=0;i<2*LENGTH;i++)                //数组不全小于零时遍历一遍求出最大子数组
        {
            temp+=arr[i];
            count++;
            if(temp>max)
            {
                max = temp;
            }
            if(temp<=0)                                //如果在数组叠加过程中和小于0  则舍去  从该部分后面重新叠加
            {
                temp=0;
                start=i+1;
                count=0;
            }
            if(count==LENGTH)
            {
                if(arr[i]<0)
                {
                    count--;
                }
                return max;
            }
        }
        return max;
    }
    
    int main()
    {
        int Arr[LENGTH];
        int Array[2*LENGTH];    //以2倍首尾相接数组代替回环数组
        int start=0;
        int count=0;
        cout<<"请输入"<<LENGTH<<"个整数(可正可负): ";
        for(int i=0;i<LENGTH;i++)
        {
            cin>>Arr[i];
        }
        int k=0;
        for(int i=0;i<2*LENGTH;i++)
        {
            Array[i] = Arr[k++];
            if(k==LENGTH)
            {
                k=0;
            }
        }
        int max = MaxSubArr(Array,start,count);
        cout<<"最大子数组序列为:";
        for(int i=0;i<count;i++)
        {
            cout<<Array[start+i]<<" ";
        }
        cout<<endl;
        cout<<"最大子数组的和为:"<<max<<endl;
        return 0;
    }

    三、运行结果

      

    四、总结

        对于相加的数组,若全为负数,只需比较大小,输出数组中最大的负数即可,如果有正有负,则依次相加比较。

      其他的和结对开发1的思想大致一样。

    五、照片

      

  • 相关阅读:
    ashx session
    jquery-tmpl中添加js判断
    问题与解决方案
    VS2013打包发布 winform程序
    Sorket通信
    加载页面菜单,原来也可以这么写
    pm2模块编写入门
    使用PouchDB来实现React离线应用
    让fetch也可以timeout
    使用JSON Schema来验证接口数据
  • 原文地址:https://www.cnblogs.com/littlechar/p/4377764.html
Copyright © 2011-2022 走看看