求陣列中和最大的子陣列

2021-06-14 15:03:52 字數 1208 閱讀 1486

【題目一】輸入乙個整形陣列,陣列裡有正數也有負數。陣列中連續的乙個或多個整數組成乙個子陣列,每

個子陣列都有乙個和。求所有子陣列的和的最大值。要求時間複雜度為o(n)。

[cpp]view plain

copy

#include "stdio.h"

#include "conio.h"

/* 求一維陣列的最大連續子陣列元素之和

入口 : a 要計算的一維整數陣列

出口 : ifrom , ito , imaxsum

分別存放 最大連續子陣列的

起始位置,結束位置(含結束位置),元素之和

如果 ito = -1 表示 原陣列是空陣列

*/void

findgreatestsumofsubarray(  

int* a,  

intn ,  

int* ifrom, 

int* ito, 

int* imaxsum )  

else

;  };  

};  

if( *ito == -1 )  

;  };  

*ito = *ifrom;  

};  

}  main()  

;  intm1, m2,sumgt ;  

findgreatestsumofsubarray( &a[0], 14, &m1,&m2, &sumgt);  

printf( "%d %d %d "

,m1,m2, sumgt) ;  

getch();  

}  

//簡化了的方法

[cpp]view plain

copy

intfindgreatestsumofsubarray(

const

int*iarray, unsigned 

intnlen, 

int&ibegin, 

int&iend)  

else

if(ncursum > ngreatestsum)  

}  return

ngreatestsum;  

}  

【題目二】給定乙個長度為n的正整數陣列,從這n個正整數中找出m個正整數,使這m個正整數之和與正數m最接近...給出這m個正整數...

求陣列中和最大的子陣列(陣列中和最大的子串)

某公司面試題,回來找的答案這個是最好的。int kadane const int array,size t length,unsigned int left,unsigned int right else return max 這裡我們需要注意,對於陣列元素全為負的情況,由於不滿足上述的兩條結論,所...

求陣列中和最大的子陣列與始末下標

using system using system.collections.generic using system.linq using system.text namespace maxsum myarray new int myarray new int myarray new int mya...

求陣列中和最大的子陣列與始末下標

using system using system.collections.generic using system.linq using system.text namespace maxsum myarray new int myarray new int myarray new int mya...