Classroom Experiment 4.1 (ring array)

Source: Internet
Author: User

Topic:

Returns the and of the largest sub-array in an integer array.

Requirements:

Enter an array of shapes with positive and negative numbers in the array.

One or more consecutive integers in an array make up a sub-array, each of which has a and.

If the array a[0] ... A[j-1] next to each other, allowing a[i-1], ... A[n-1],a[0] ... A[J-1] and the largest.

Returns the position of the largest subarray at the same time. The maximum value for the and of all sub-arrays.

Design ideas:

This is an iterative development, based on the previous task, in the previous program with a function, is to divide the ring array into n sub-arrays, each number of the ring array can be the first number of each array, and then in order to find out the maximum sub-array in each array results, The maximum value is calculated by comparison. The implementation of the ring array can be realized by using the data structure's Union table.

Code:

#include <iostream>using namespacestd;#defineN 100intShuzu (intHintArray2[n]) {    inti; intH1; H1=h; intArray3[n]; array3[0]=array2[0]; intmaxsize=array3[0];  for(intE=0; e<=h-1; e++) {Array3[e]=Array2[e]; MaxSize=Array3[e];  for(i=e+1; i<=h1;i++)        {        if(array3[i-1]>0) {Array3[i]=array3[i-1]+Array2[i]; MaxSize=max (maxsize,array3[i-1]); }        Else{Array3[i]=Array2[i]; }} ARRAY2[H1]=Array2[e]; H1=h1+1; } cout<<"the maximum number of sub-arrays is:"<<maxsize; cout<<"the array is:"<<Endl; return 0;}intMain () {inti,j; cout<<"Please enter the size of the ring array: (1-10)"<<Endl; CIN>>J; intArray1[n]; cout<<"Please enter"<<j<<"Number:"<<Endl;  for(i=0; i<j;i++) {cin>>Array1[i];        } Shuzu (J,array1); return 0;}

Teammate Tanono Address: http://www.cnblogs.com/tyyhph/

Classroom Experiment 4.1 (ring array)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.