One-dimensional ring array

Source: Internet
Author: User

A topic and requirements:

Title: Returns the maximum number of sub-arrays in an integer array and

Requirement (new addition): ① if array a[0] ... A[j-1] Next to the end, allow A[i-1] ... A[n-1],a[0] ... A[J-1] The sum of the largest; ② returns the position of the largest subarray at the same time.

Two design ideas:

Our idea of this topic is to continue the thought of the previous one-dimensional array, only to make a little change, such as the number is five, 1,2,3,-1,-1. The array space we open is 10,

Save the five numbers again, that is, 1,2,3,-1,-1,1,2,3,-1,-1. Using loops is the number of cycles per five, such as the first loop to find the 1,2,3,-1,-1

Continuous maximum and, the second loop finds the maximum number of consecutive numbers in the 2,3,-1,-1,1, and so on.

Three experimental code

#include <iostream.h>#include<time.h>#include<stdlib.h>intMain () {intarry[ -]; intStart,end,i; LongSum,max; cout<<"Please input numbers:"<<Endl;  for(i=0;i<Ten; i++) {cin>>Arry[i]; Arry[i+Ten]=Arry[i]; } Max=arry[0]; Start=0; End=0;  for(intj=0;j<Ten; j + +) {sum=0;  for(intk=j;k<Ten+j;k++) {sum=sum+Arry[k]; if(sum>max) {Max=sum; Start=j+1; End=k+1; } }} cout<<"MAX is"<<" "<<max<<Endl; cout<<"START:"<<" the"<<" "<<start<<"th"<<" "<<" Number"<<"    "<<"END:"<<" the"<<" "<<end%Ten<<"th"<<" "<<" Number"<<Endl; cout<<"These numbers is:";  for(intC=0; c<end-start+1; C + +) {cout<<arry[start+c-1]<<"  "; }}

Four experiments

Five experimental Thoughts

Our greatest impression of the experiment is that if a program to understand, add some conditions, and then make some changes, can be from the most primitive procedures to find a breakthrough,

This procedure our main idea and last time no difference, only made a small change on the success, classroom made out, so also very excited, of course, and the partner Smart

The head is not open to the relationship.

One-dimensional ring array

Related Article

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.