Seeking a maximum value of a given array and the maximum subsequence (code implementation)

Title: Given a set of integers, the maximum value is obtained sequences and the set of numbers, as long as the sub-sequence and find the maximum, the maximum value of the corresponding sequence need not obtained.

And maximum subsequence: integer sequence A1, A2, ... An (there may be a negative number), seeking A1An is a sequence AiAj, so that the maximum and Ai to Aj,.

For example: the sequence: 0, -3, 6, 8, -20, 21, 8, -9, 10, -1, 3, 6, 5, and the maximum of 43 sequences.

 

code show as below:

 

. 1 #include <stdio.h>
 2 #include <stdlib.h>
 . 3  #define GET_ARRAY_LEN (Array, len) {len = the sizeof (Array) / the sizeof (Array [0]);} // define parameters of a macro the variable len length of the array is stored 
. 4  
. 5  int main ()
 . 6  {
 . 7      int array [] = { 0 , - . 3 , . 6 , . 8 , - 20 is , 21 is , . 8 , - . 9 , 10 , - . 1 , . 3 , . 6 , . 5 };
 . 8      int i, j, len, max;
 9     GET_ARRAY_LEN(array, len);
10     max = array[0];    //初始化最大值
11 
12     for(i = 0; i < len; i++){
13         int temp = array[0];
14         for(j = i+1; j < len; j++){
15             temp += array[j];
16             if(temp > max)
17                 max    = temp;
18         }
19     }
20 
21     printf(" Maximum sequences and is:% D \ n- " , max);
 22 is      return  0 ;
 23 is }

 

The above code is worth learning and remembering the code line 3 is where: a length of the memory macro is defined with parameters, the array of the variable len.

 

 #define GET_ARRAY_LEN(array, len) {len = sizeof(array) / sizeof(array[0]);}

 

Wherein sizeof (array) indicates the size of the entire array, but sizeof (array [0]) in a size of the array element is expressed. It is the size of the entire length of the array divided by the array size in which it obtained element. Note that the size of each element of the array is the same, the length of the array can be thus determined.

Guess you like

Origin www.cnblogs.com/lucky-tam1201/p/11204629.html