Algorithm for seeking maximum subsequences

xiaoxiao2021-04-07  361

problem:

A sequence is given, and a continuous subsequence is given to make its value among all subsequent sequences, each element can be negative.

analysis:

With the induction method, when we know (

X1 x2 ... x

After the maximum sequence of i-1), then add

After the XI will take the two cases: First, the original maximum is not affected; second, including the XI suffixing sequence, we need to retain these two maximum in the process of traversal.

#include

#include

Double Find_Subserial

Double x [], int N)

{

Double global_max = 0; // save the global max value

Double Suffix_max = 0; // Save the Suffix Max Value

INT I;

For (i = 0; i

{

IF (SUFFIX_MAX X [I]> Global_max)

{

GLOBAL_MAX = SUFFIX_MAX = SUFFIX_MAX X [I];

}

ELSE IF (SUFFIX_MAX X [I]> 0)

{

SUFFIX_MAX = SUFFIX_MAX X [I];

}

Else

{

SUFFIX_MAX = 0;

}

}

RETURN GLOBAL_MAX;

}

Int main

INT Argc, Char * Argv [])

{

Double * temp;

INT I;

Temp = (Double *) Malloc ((argc-1) * sizeof (double);

For (i = 1; i

{

Temp [I-1] = ATOF (Argv [i]);

Printf ("% g", TEMP [I-1]);

}

Printf ("/ n");

Printf ("THE MAX IS% G / N", Find_Subserial (TEMP, ARGC-1);

Return 0;

}

转载请注明原文地址:https://www.9cbs.com/read-132534.html

New Post(0)