简体   繁体   English

找到具有最大和的连续子数组

[英]Finding the contiguous sub array with max sum

here's my program to find the maximum sum of a sub array (contiguous) from a given array.这是我的程序,用于从给定数组中查找子数组(连续)的最大和。 it's very easy using kadane's algorithm.使用 kadane 的算法很容易。

#include <iostream>
#include <cstdio>

using namespace std;

int kadane(int a[], int n) {

    int max_ending_here = a[0], max_till_now = a[0];

    int _start = 0, _end;

    bool s=true;

    for (int i = 1; i < n; ++i)
    {
        max_ending_here = max(a[i], max_ending_here + a[i]);

        if(max_ending_here + a[i] > a[i] && s==false) _start = i, s=true;

        max_till_now = max(max_ending_here, max_till_now);

        if(max_ending_here + a[i] < a[i] && s==true) _end=i-1,s=false;
    }

     printf("S = %d , E = %d\n",_start,_end);

    return max_till_now;
}

int main(int argc, char const *argv[])
{
    //int a[10] = {1,-3,2,-5,7,6,-1,-4,11,-23};
    int a[6] = {-8,-1,-1,-1,-1,-5};
    int m = kadane(a, 6);
    printf("%d\n",m);
    return 0;
}

but i also want to find the start and end position of that contiguous sub-array which has the max sum.但我也想找到具有最大总和的连续子数组的开始和结束位置。 i tried adding couple of lines in above program to do that, but it didn't work.我尝试在上面的程序中添加几行来做到这一点,但它没有用。 so my question is how can i get the start and end positions of that sub-array having max sum?所以我的问题是如何获得具有最大总和的子数组的开始和结束位置? thanks.谢谢。

Extend the function signature like this:像这样扩展函数签名:

int kadane(int a[], int n, int *start, int *end)

At the end of the function, before the return set the two parameters like this:在函数的末尾,在返回之前设置两个参数,如下所示:

    *start = _start;
    *end = _end;
    return max_till_now;
}

And call it like this:并这样称呼它:

int start, end;  
int m = kadane(a, 6, &start, &end);
printf("sum: %i, start %i, end: %i\n",m, *start, *end);

Try to use this code as a base to do what you want.尝试使用此代码作为基础来做你想做的事。 Just ignore some words that are in Portuguese (my native language).只需忽略一些葡萄牙语(我的母语)单词。

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

void seg_max(int *v, int n, int & x, int &y , int & max){
 int i,j;
 int soma; 
 max = -1; 
 for(i=0;i<n;i++){
  soma = 0;
  for(j=i;j<n;j++){
   soma += v[j];
   if( soma > max ){
    max = soma;
    x = i;
    y = j;
   }
  }
 }
}

int main(){
int x,y,max;
int v[] = {-2,1,-3,4,-1,2,1,-5,4};
seg_max(v,9,x,y,max);
printf("max sum [%d-%d] with the sum equal to %d\n", x,y,max);
}

To pass more out of the function, use pointers.要从函数中传递更多信息,请使用指针。 Below should work.下面应该工作。

#include <cstdio>

using namespace std;

int kadane(int a[], int n, int* start, int* end ) {

    int max_ending_here = a[0], max_till_now = a[0];

    bool s=true;

    for (int i = 1; i < n; ++i)
    {
        max_ending_here = max(a[i], max_ending_here + a[i]);

        if(max_ending_here + a[i] > a[i] && s==false) {
             *start = i;
             s=true;
        }

        max_till_now = max(max_ending_here, max_till_now);

        if(max_ending_here + a[i] < a[i] && s==true) {
            *end = i-1;
            s = false;
        }
    }

    return max_till_now;
}

int main(int argc, char const *argv[])
{
    //int a[10] = {1,-3,2,-5,7,6,-1,-4,11,-23};
    int a[6] = {-8,-1,-1,-1,-1,-5};
    int start = 0, end = 0;
    int m = kadane(a, 6, &start, &end);
    printf("Max: %d, Start: %d, End: %d\n",m, start, end);
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM