Solutions_to_Introduction_to_Algorithm_3nd_Exercise_4

来源:互联网 发布:2016最新网络流行词汇 编辑:程序博客网 时间:2024/06/06 02:48
4.1-1 What does FIND-MAXIMUM-SUBARRAY return when all elements of A are negative?
  The biggest negative one.
4.1-2 Write pseudocode for the brute-force method of solving the maximum-subarray problem. Your procedure should run inΘ(n^2);

typedef struct{int max_left;int max_right;int max;}maxinfo;maxinfo bf_MaxSubArray(int *a,int n){maxinfo max;max.max_left= 0;max.max_right= 0;max.max= MIN_INT;int tmp=0;for(int i=0;i<n;i++){for(int j=i;j<n;j++){tmp += a[j];if(tmp > max.max){max.max = tmp;max.max_left = i;max.max_right = j;}}tmp = 0;}return max;}


原创粉丝点击