The complexity of the algorithm is seen by a small program

Source: Internet
Author: User

was asked to write a procedure today, the topic is as follows; the price of gold in the last four weeks within 20 trading days, the maximum difference in the last 20 days?

A[20]={2,5,1,6,7,9,10,18...1}

What I was thinking was the difference between the two days of trading, the difference between the 1th and 2nd days, the difference between 2nd and 3rd days, the difference between the first day and the third day, the maximum between them??

So, confidently wrote:

    int a[20]={2,5,1,6,7,9,10,18...1};    int b[19]={0};    int max=0;    for (int m=1;m<20;m++)    {int tempmax=0;//stores the difference Value data for (int n=0;n<19;n++) {b[n]=0;} Calculates the difference for (int i=m;i<m;m++) {b[i]=a[i]-a[i-1];} The maximum value for the difference value for (int n=0;n<19;n++) {if (B[n]>tempmax) {              tempmax=b[n];}} if (Tempmax>max) {Max=tempmax;}    }

Two days of difference, my algorithm complexity n (n-1) * (n-2) **1;

Students, do I write the code right? How well the code is written!! I'm proud of it.

The moment is coming, I'm going to interrupt an ad.

The best power Model detection Tool, the best power Communication test Tool (http://www.mms61850.com/_d4.htm);

The best power Model detection Tool, the best power Communication test Tool (http://www.mms61850.com);

The best power Model detection Tool, the best power Communication test Tool (http://www.mms61850.com);

The best power Model detection Tool, the best power Communication test Tool (http://www.mms61850.com);

The best power Model detection Tool, the best power Communication test Tool (http://www.mms61850.com);

The best power Model detection Tool, the best power Communication test Tool (http://www.mms61850.com);

The best power Model detection Tool, the best power Communication test Tool (http://www.mms61850.com);

I want to be any two days between the maximum difference, not necessarily two days between the difference, we do not two days between the difference, turn this bend over, good to do.

I get the maximum value and the minimum value, then the difference between them is not the maximum difference between any two days??

1 inta[ -]={2,5,1,6,7,9,Ten, -...1};2     //Maximum Value3     inttempmax=a[0];4     //Minimum Value5     inttempmin=a[0];6     intMax;7      for(intm=1;m< -; m++)8     {9         if(a[m]>Tempmax)Ten         { Onetempmax=A[m]; A         } -         Else if(a[m]<tempmin) -         { thetempmin=A[m]; -         } -     } -     //as a result, even if we need +Max= tempmax-tempmin;

The complexity of the algorithm is n.
Different ways of implementation, the gap is very large ...

Oh, think more, the way is always more difficult!

The complexity of the algorithm is seen by a small program

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.