标签:rand define 分享 ++ 最大子数组和 class std printf sizeof
程序代码:
#include<stdio.h> #define M 5 #define N 4 int maxSubArray(int arr[], int len) { int i, sum = arr[0], b = 0; for (i = 0; i<len; ++i) { if (b>0) b += arr[i]; else b = arr[i]; if (b>sum) sum = b; } return sum; } int maxSubMatrix(int n, int m, int array[M][N]) { int i, j, h, max, sum = -100000; int b[100]; for (i = 0; i < n; i++) { memset(b, 0, sizeof(b)); for (j = i; j < n; j++) { for (h = 0; h<m; h++) { b[h] += array[j][h]; } max = maxSubArray(b, h); if (max>sum) sum = max; } } return sum; } int main() { int arr[M][N] ; printf("随机产生二维数组为\n"); srand(time(0)); int i,j; for (i = 0; i < M; i++) { for (j = 0; j < N; j++) { arr[i][j] = rand() % 100-50; printf("%d ",arr[i][j] ); } printf("\n"); } printf("最大子数组和为:%d\n",maxSubMatrix(M, N, arr)); system("pause"); return 0; }
运行结果:
工作照片:
标签:rand define 分享 ++ 最大子数组和 class std printf sizeof
原文地址:https://www.cnblogs.com/YSong666/p/9826184.html