(4) C language--to find the maximal continuous subsequence and

Source: Internet
Author: User

Topic:

Enter a set of integers to find out the set of number sub-sequences and the maximum values. That is, only the maximum number of sub-sequences is required, and the largest sequence is not to be obtained. For example:

Sequence:-2 11-4 13-5-2, the maximum subsequence and 20.

Sequence:-6 2 4-7 5 3 2-1 6-9 10-2, the maximum subsequence and 16.

1.

1 /*2 algorithm One: Poor Lifting method (3 for)3 complexity of Time: O (n^3)4 5 */6#include <stdio.h>7#include <malloc.h>8 9 intMax =0;Ten intFind_max (intLenintarr[]) { One     intI, J, K, sum; A      for(i=0; i<len; i++){ -          for(j=i; j<len; J + +){ -sum =0; the              for(k=i; k<=j; k++){ -Sum + =Arr[k]; -             } -             if(Sum >Max) { +Max =sum; -             } +         } A     } at     returnMax; -  - }  -  - intMain () { -     intI, Len, *arr; inprintf"Please enter the length of the array:"); -scanf"%d",&len); toArr = (int*)malloc(sizeof(int)*len); +printf"Please enter the value of the array:"); -      for(i=0; i<len; i++){ thescanf"%d",&arr[i]); *     } $ Find_max (Len,arr);Panax Notoginsengprintf"Maximum continuous sub-sequence and:%d\n", Max); -      the     return 0; +}

2.

1 /*2 algorithm two: optimization of algorithm one (2 for)3 complexity of Time: O (n^2)4 */5#include <stdio.h>6#include <malloc.h>7 8 intMax =0;9 intFind_max (intArr[],intNintLen) {Ten       intI, sum =0; One      for(I=n; i<len; i++){ ASum + =Arr[i]; -         if(Sum >Max) { -Max =sum;  the         } -     } -     returnMax; - }  +  - intMain () { +     intI, Len, *arr; Aprintf"Please enter the length of the array:"); atscanf"%d",&len); -Arr = (int*)malloc(sizeof(int)*len); -printf"Please enter the value of the array:"); -      for(i=0; i<len; i++) -     { -scanf"%d", &arr[i]); in     } -  to      +      for(i=0; i<Ten; i++){ - Find_max (Arr,i, Len); the     } *          $printf"maximum consecutive subsequence and:%d \ n", Max);Panax Notoginseng  -     return 0; the}

3.

1 /*2 algorithm three: on-line algorithm3 time Complexity: O (n)4 */5#include <stdio.h>6#include <malloc.h>7 8 intMax =0;9 intFind_max (intLenintarr[])Ten { One     intI, sum =0; A      for(i=0; i<len; i++) -     { -Sum + =Arr[i]; the         if(Sum >Max) -         { -Max =sum; -}Else if(Sum <0){ +sum =0; -         }     +     } A  at     returnMax; - } -  - intMain () { -     intI, Len, *arr; -printf"Please enter the length of the array:"); inscanf"%d",&len); -Arr = (int*)malloc(sizeof(int)*len); toprintf"Please enter the value of the array:"); +      for(i=0; i<len; i++) -     { thescanf"%d", &arr[i]); *     } $ Find_max (len, arr);Panax Notoginsengprintf"maximum consecutive subsequence and:%d \ n", Max); -     return 0; the}

Reference: http://blog.163.com/kevinlee_2010/blog/static/169820820201010495438247/

(4) C language--to find the maximal continuous subsequence and

Related Article

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.