darkflammme 2018-03-01 11:44 采纳率: 100%
浏览 1279
已采纳

递归 算法 最大子数组

虽然写的非常差但还是我这个小白自己亲手写出来的,一边翻书一边谷歌..... 花了好多小时,虽然很罗嗦而且希望大家能帮我找一下我的find_maximum_subarry的错误,其余两个函数我都试过了应该是对的

#include

typedef struct all_of_sum
{
int aa;
int bb;
int cc;
}all_sum;
all_sum all;

int max(int one, int two, int three)
{
int current_max = one;
if(current_max < two)
{
current_max = two;
if(current_max < three)
current_max = three;
}
else if(current_max >= three)
{
if(current_max < three)
current_max = three;
}

return current_max;

}

all_sum find_crossing_sum(int *array,int a,int mid,int b)
{
int maxleftsum = -999999,maxrightsum = -999999,currentleft = 0;
int currentright = 0,i = 0,j = 0;

for(i = a; i <=mid;i++)
{
    currentleft = currentleft + array[i];
    if(currentleft > maxleftsum)
        maxleftsum  = currentleft;
    };

for(j = mid+1; j <= b;j++)
{
    currentright = currentright + array[j];
    if(currentright > maxrightsum)
        maxrightsum  = currentright;
    };
all.aa = i-1;
all.bb = j-1;
all.cc = maxleftsum + maxrightsum;

return all;

}

typedef struct find_maxium
{
int low;
int high;
int ddd;
} maxium;
maxium mm;

maxium find_maximum_subarry(int*array,int a,int b)
{
int mid = 0,left_low = 0,left_high = 0,left_sum = 0;
int right_low = 0,right_high = 0,right_sum = 0;
int cross_low = 0,cross_high = 0,cross_sum = 0;
if(a == b)
{
mm.low = a;
mm.high = b;
mm.ddd = array[a];
return mm;
}
else
{
mid = (a + b)/2;
left_low = find_maximum_subarry(array,a,mid).low;
left_high = find_maximum_subarry(array,a,mid).high;
left_sum = find_maximum_subarry(array,a,mid).ddd;

     right_low = find_maximum_subarry(array,mid+1,b).low;
     right_high = find_maximum_subarry(array,mid+1,b).high;
     right_sum = find_maximum_subarry(array,mid+1,b).ddd;

    cross_low = find_crossing_sum(array,a,mid,b).aa;
    cross_high = find_crossing_sum(array,a,mid,b).bb;
    cross_low = find_crossing_sum(array,a,mid,b).cc;
}
if(max(left_sum,right_sum,cross_sum) == left_sum)
    {
        mm.low = left_low;
        mm.high = left_high;
        mm.ddd = left_sum;
    }
else if(max(left_sum,right_sum,cross_sum) == right_sum)
    {
        mm.low = right_low;
        mm.high = right_high;
        mm.ddd = right_sum;
    }
else if(max(left_sum,right_sum,cross_sum) == cross_sum)
    {
        mm.low = cross_low;
        mm.high = cross_high;
        mm.ddd = cross_sum;
    }
return mm;
}
  • 写回答

1条回答 默认 最新

  • threenewbee 2018-03-01 14:55
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 易优eyoucms关于二级栏目调用的问题
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题