1 /************************************************************************/
2 /* 计算数组中连续元素和的最大值所在的区间
3 /************************************************************************/
4 #include "stdio.h"
5 #include "time.h"
6 #include "stdlib.h"
7
8 #define PRINT 0 /* 是否打印数组 */
9 #define SRAND 1 /* 是否打开随机数发生器 */
10 #define ARRAY_MAX_LEN 210000 /* 数组大小 */
11 #define ALG_1 0
12 #define ALG_2 1
13
14 /* 打印数组元素 */
15 #define PRINTARRAY() \
16 {\
17 for (idx=0; idx < ARRAY_MAX_LEN; idx++)\
18 {\
19 printf("%d ", x[idx]);\
20 }\
21 }
22
23 #define MAX(a,b) ((a > b) ? a : b)
24
25 void main()
26 {
27 /* 随机生成一批数字 */
28 int x[ARRAY_MAX_LEN];
29 int idx=0;
30
31 #if SRAND
32 srand((unsigned)time(NULL));
33 #endif
34
35 printf("generating %d rand numbers...\n", ARRAY_MAX_LEN);
36 for (idx=0; idx < ARRAY_MAX_LEN; idx++)
37 {
38 if (idx % 2 == 0)
39 {
40 x[idx] = rand() % 3;
41 }
42 else
43 {
44 x[idx] = -rand() % 8;
45 }
46 }
47 printf("generating done!\n");
48
49 /* 打印数组元素 */
50 #if PRINT
51 PRINTARRAY();
52 #endif
53
54 /* 计时开始 */
55 clock_t start, finish;
56 double duration;
57 start = clock();
58
59 int maxsofar = 0;
60 int sum = 0;
61
62 printf("computing range-sum-max...\n");
63 #if ALG_1
64 /* 计算过程 (1) */
65 for (int i = 0; i < ARRAY_MAX_LEN; i++)
66 {
67 for (int j = i; j < ARRAY_MAX_LEN; j++)
68 {
69 sum += x[j];
70 maxsofar = MAX(maxsofar, sum);
71 }
72 sum = 0;
73 }
74 #endif
75 /* 计算过程 (2) */
76 #if ALG_2
77 int maxendinghere = 0;
78 for (int i = 0; i < ARRAY_MAX_LEN; i++)
79 {
80 maxendinghere = MAX(maxendinghere + x[i], 0);
81 maxsofar = MAX(maxendinghere, maxsofar);
82 }
83 #endif
84 /* 计时结束 */
85 finish = clock();
86 duration = (double)(finish - start) / CLOCKS_PER_SEC;
87
88 /* 统计输出 */
89 printf("duration: %f seconds\n",duration);
90 printf("range-sum-max: %d\n", maxsofar);
91 getchar();
92 }
2 /* 计算数组中连续元素和的最大值所在的区间
3 /************************************************************************/
4 #include "stdio.h"
5 #include "time.h"
6 #include "stdlib.h"
7
8 #define PRINT 0 /* 是否打印数组 */
9 #define SRAND 1 /* 是否打开随机数发生器 */
10 #define ARRAY_MAX_LEN 210000 /* 数组大小 */
11 #define ALG_1 0
12 #define ALG_2 1
13
14 /* 打印数组元素 */
15 #define PRINTARRAY() \
16 {\
17 for (idx=0; idx < ARRAY_MAX_LEN; idx++)\
18 {\
19 printf("%d ", x[idx]);\
20 }\
21 }
22
23 #define MAX(a,b) ((a > b) ? a : b)
24
25 void main()
26 {
27 /* 随机生成一批数字 */
28 int x[ARRAY_MAX_LEN];
29 int idx=0;
30
31 #if SRAND
32 srand((unsigned)time(NULL));
33 #endif
34
35 printf("generating %d rand numbers...\n", ARRAY_MAX_LEN);
36 for (idx=0; idx < ARRAY_MAX_LEN; idx++)
37 {
38 if (idx % 2 == 0)
39 {
40 x[idx] = rand() % 3;
41 }
42 else
43 {
44 x[idx] = -rand() % 8;
45 }
46 }
47 printf("generating done!\n");
48
49 /* 打印数组元素 */
50 #if PRINT
51 PRINTARRAY();
52 #endif
53
54 /* 计时开始 */
55 clock_t start, finish;
56 double duration;
57 start = clock();
58
59 int maxsofar = 0;
60 int sum = 0;
61
62 printf("computing range-sum-max...\n");
63 #if ALG_1
64 /* 计算过程 (1) */
65 for (int i = 0; i < ARRAY_MAX_LEN; i++)
66 {
67 for (int j = i; j < ARRAY_MAX_LEN; j++)
68 {
69 sum += x[j];
70 maxsofar = MAX(maxsofar, sum);
71 }
72 sum = 0;
73 }
74 #endif
75 /* 计算过程 (2) */
76 #if ALG_2
77 int maxendinghere = 0;
78 for (int i = 0; i < ARRAY_MAX_LEN; i++)
79 {
80 maxendinghere = MAX(maxendinghere + x[i], 0);
81 maxsofar = MAX(maxendinghere, maxsofar);
82 }
83 #endif
84 /* 计时结束 */
85 finish = clock();
86 duration = (double)(finish - start) / CLOCKS_PER_SEC;
87
88 /* 统计输出 */
89 printf("duration: %f seconds\n",duration);
90 printf("range-sum-max: %d\n", maxsofar);
91 getchar();
92 }