Ring array of pair development

Source: Internet
Author: User

I. Topics and Requirements

Title: 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. Requires a time complexity of O (n)

 
Second, design ideas

1, on the last one-dimensional array of the largest subarray of the problem, to expand, continue to use a similar summation method

2, should be subject to the requirements, this time an array to end-to-end, allow a[i-1], ... A[n-1], a[0] ... A[J-1] and the largest, so we use the class method to find the maximum value, the array of elements to expand, that is, the array in addition to the last element from the new stitching to the tail, forming a ring.

Third, the source code

 #include <iostream.h> #define N + int main (int argc, char* argv[]) {int arry[n]; int length;     int max[n],max1;     int maxweizhi[n];     int value (int arry[],int length);     cout<< "Please enter the number of integers to compare:" <<endl;     cin>>length;     cout<< "Please enter integer" <<endl;         for (int i=0;i<length;i++) {cin>>arry[i];     } int num=0;         for (int j=length;j<2*length-1;j++) {Arry[j]=arry[num];     num++;     } cout<< "A ring array formed by these numbers";     Value (arry,2*length-1); return 0;     } int value (int arry[],int length) {int max[n],max1;     int maxweizhi[n];         for (int j=0;j< (length+1)/2;j++) {int sum=0;         max1=-9999999;         int z=0;             for (int i=j;i< (length+1)/2+j;i++) {sum=sum+arry[i];                 if (sum>=max1) {max1=sum;             z++;         }} max[j]=max1;   Maxweizhi[j]=z;       } int fmax=max[0];     int q=0;             for (int i=0;i< (LENGTH+1/2); i++) {if (Max[i]>fmax) {fmax=max[i];         Q=i;      }} int weizhi=maxweizhi[q];     cout<< "Maximum sub-array:" <<endl;     for (int num=q;num<q+weizhi;num++) {cout<<arry[num];     } cout<< "and as:" <<fmax<<endl; return 0; }

Iv. Results of operation

V. Summary

Through this experiment, I realized the strength of unity. At first, my friends and I have no idea, in class, after the inspiration of classmates and teachers, to solve the method has a train of thought. This also allows us to program under class

Save a lot of effort. Through this experiment, but also reflect on their own problems. Others can come up with simple ways, and the reason we can't think of them is that there are fewer exercises, fewer opportunities to meet and solve problems. So, also

It takes a lot of practice. Each time solves the problem, should be in time the reflection summary, the method solves which comes, accumulates unceasingly.

Ring array of pair development

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.