Maximum sub-array-productivity

Source: Internet
Author: User

1#include <iostream>2#include <ctime>3 #defineN 2000//macro Definition4 using namespacestd;5 voidMain ()6 7 {8     intA[n];//array of integers9     intF//range of ValuesTen     intI, J, K;//Control Loop Variables One     intSum, Max;//and the maximum value A     intM, N;//record sub-array -clock_t start, end;//record the start and end times -cout <<"Please enter a range of values:"; theCIN >>F; -start = Clock ();//assign the current system's time call to start -Srand ((unsigned) time (NULL));/*Initializes a random number for the seed - There's no such thing as code + The resulting random number should be the same plus this code - The random number generated is random.*/ +      for(i =0; i<n; i++)//control Loops, A     { atA[i] = (rand ()%2? -1:1) *rand ()% F;//Generate random numbers -cout << A[i] <<"   ";//output one-dimensional arrays -     } -cout <<Endl; -max = a[0];//The first number in the array as the maximum value first -     //The following is a comparison of the maximum value in      for(i =0; i<n; i++) -     { to          for(j =0; J <= I; J + +) +         { -sum =0;//initialize the and of the word group the              for(k = j; k <= i; k++)//Loop Variable *sum = sum +A[k];  $             if(sum>max)Panax Notoginseng             { -max = sum;//Update Max: The and plus next number from the first subarray becomes the and of the new sub-array, comparing the maximum value them =J;  +n = i;//can't record the i,j in the loop.  A             } the         } +     } -end = Clock ();//End of Time $cout <<"run time is:"<<Double((End-start))/ +<<"seconds"<< Endl;//Run Time $cout <<"the maximum number of sub-arrays and is:"<< Max <<Endl; -cout <<"the sub-array is the first"<< m +1<<"number, to the first"<< n +1<<"number. "<<Endl; -cout <<"the sub-arrays are:"; the      for(i = m; i <= N; i++) -     {Wuyicout << A[i] <<"  ";  the     } -cout <<Endl; Wu}

Experiment:

Array Size: 5000

Running time: 57.202s


Design ideas: After the macro defines the array of operands, the input array of the range of values, starting from the definition of the first sub-array to compare and find the maximum value of the sub-array, and record the start and end of time.

Implementation time Flow: Wednesday (April 8)

 
events time period time
design ideas < td> 1h30m"
Online find example 4:30-5:00 30m
explanatory statement 6:00-8:00 2h
8:00-8:30

Summary: What will not, I should learn to ask others, check information, can not always keep the computer in a daze. And try to remember what you've seen.

Maximum sub-array-productivity

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.