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.