zoukankan      html  css  js  c++  java
  • 求一维循环数组最大子数组的和

    结对成员:信1201-1班 于海洋   袁佩佩

    一.题目与要求

    题目:返回一个整数数组中最大子数组的和。

    要求: 输入一个整形数组,数组里有正数也有负数。 数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和。如果数组A[0]……A[j-1]首尾相邻,允许A[i-1], …… A[n-1], A[0]……A[j-1]之和最大。 同时返回最大子数组的位置。

    二.设计思路

    利用之前的返回一个整数数组最大子数组的和程序的思路,不过这里将数放到一个链表里,首尾相连,来求最大子数组的和。

    三.源代码

    // 一维数组.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include<iostream.h>
    #define num 5
    /*链表数据结构*/
    typedef struct LNode
    {
        int data;
        struct LNode *next;
    }LNode,*LinkList;
    /*链表的初始化*/
    void InitList(LinkList &L)
    {
        L=new LNode;
        L->next=NULL;
    }
    /*链表数据的插入*/
    void InsertList(LinkList &L)//建立循环链表
    {
        LNode *head,*temp;
        head=L;
        cout<<"请输入"<<num<<"个数字:";
        for(int i=0;i<num;i++)
        {
            temp=new LNode;
            cin>>temp->data;
            temp->next=NULL;
            head->next=temp;
            head=head->next;
        }
        head->next=L->next;                //首尾相连,建立循环链表
    }
    void output(LinkList L)
    {
        for(int i=0;i<num;i++)
        {
            cout<<L->next->data<<" ";
            L=L->next;
        }
    }
    int main( )
    {
        int max,sum,flag=0;                //sum是字数组的和,max是最大的子数组的和
        int ordern=0,orderx=0;
        LinkList L;
        LNode *temp,*temp1,*temp2,*head;
        InitList(L);
        InsertList(L);                    //由用户往链表中插入数据
        temp=L->next;
        max=L->next->data;                //max初值是链表中第一个数
        for(int i=0;i<num;i++,temp=temp->next)
        {
            temp2=temp;
            for(int j=0;j<num;j++,temp2=temp2->next)
            {
                for(int k=j;k<num;k++)
                {
                    sum=0;
                    temp1=temp2;
                    for(int h=j;h<=k;h++,temp1=temp1->next)
                    {
                         sum=sum+temp1->data;
                    }
                     if(max<sum)        //将最大值赋给max,并且保存当时的序号 
                     {
                         max=sum;
                         ordern=j;
                         orderx=k;
                         head=temp;
                         flag=i;        //用来求取最大值的时候的链表的情况
                     }
                }
            }
        }
        temp=L->next;
        cout<<"最大字数组是:";
        for(i=0;i<(flag+ordern);i++)    //找出取得最大值的时候的子数组的第一个数
        {
            temp=temp->next;
        }
        for(int j=0;j<(orderx-ordern+1);j++,temp=temp->next)//将取得最大和的子数组元素输出
        {
            cout<<temp->data<<"  ";
        }
        cout<<endl<<"最大子数组的和是:"<<max<<endl;;
    
        return 0;
    }

    四.结果及截图

    五心得体会

      我们在各自说出自己的想法的时候,发现有一些地方可以互补,整合之后的要比我们自己的好一些,代码也简练一些,在调试的时候,有一些错误,但是我们自己说着说着错误就找到了,效率挺高的。

    六.合作照

  • 相关阅读:
    animation关键帧动画语法
    border-image
    CSS3之box-shadow
    border-radius编程练习1-3
    CSS之border-radius
    CSS3之径向渐变
    CSS3之线性渐变(linear gradients)
    CSS之background——背景与渐变练习题
    background-image
    background-color
  • 原文地址:https://www.cnblogs.com/menglikanhualuo/p/4373039.html
Copyright © 2011-2022 走看看