Maximum and time metric of continuous vector in C language _c language

Source: Internet
Author: User

This article analyzes the maximum and time measurement of the C-language continuum vector, and shares it for everyone's reference. The specific method is as follows:

#include <stdio.h> #include <time.h> #include <stdlib.h> #define SCALE 3000 int maxnum (int a, int b);
 int main (int argc, char const *argv[]) {FILE *fp;
 fp = fopen ("maximum.in", "R"); int x[] = {1,12,-11,10,-65,54,22,-9,21,5,48,5,-8,-2,56,54,-88,-5,2,-8,554,-56,35,-55,555,-65,-545,-23,48,-5,88,
 -56,16,-8};
 int *x = (int *) malloc (sizeof (int) * (scale+1));
 int XI = Scale,a = 0,num_in = 0;
 while (xi--) {fscanf (FP, "%d", &x[a++]);

 clock_t start, end;

 Algorithm-1 cube*** start = clock ();
 int max = 0;
 int length = SCALE;
 int i,j,k;
   for (i = 0; i < length; ++i) {for (j = i; j < length; ++j) {int sum = 0;
   for (k = i; k <= J; ++k) {sum + = x[k];
  max = Maxnum (max, sum);
 }//Long num = 10000000L;

 while (num--);
 end = Clock ();
 Double times = (double) (End-start)/clocks_per_sec;
 Double dend = (double) end;
 printf ("\n***algorithm-1 cube***\n");
 printf ("End:%f\n", dend); printf ("Time Consuming:%F\n ", times);
 printf ("%d\n", Max);
 Algorithm-2 square*** start = clock ();
 max = 0;
  for (i = 0; i < length; ++i) {int sum = 0;
   for (j = i; j < length; ++j) {sum + = x[j];
  max = Maxnum (max, sum);
 } end = Clock ();
 Times = (double) (End-start)/clocks_per_sec;
 DEnd = (double) end;
 printf ("\n***algorithm-2 square***\n");
 printf ("End:%f\n", dend);
 printf ("Time Consuming:%f\n", times);
 printf ("%d\n", Max);
 Algorithm-3 linear*** start = clock ();
 max = 0;

 int max_end_here = 0;
  for (i = 0; i < length; ++i) {max_end_here = Maxnum (Max_end_here + x[i], 0);
 max = Maxnum (max, max_end_here);
 end = Clock ();
 Times = (double) (End-start)/clocks_per_sec;
 DEnd = (double) end;
 printf ("\n***algorithm-3 linear***\n");
 printf ("End:%f\n", dend);
 printf ("Time Consuming:%f\n", times);
 printf ("%d\n", Max);
 Free (x);
 x = NULL;
return 0;

 int maxnum (int a, int b) {return a > b a:b;}

Interested friends can test and run the example of this article to deepen understanding. I hope this article will help you with the learning of C programming.

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.