實現內容:假如有這樣乙個陣列,a = ;要求得到乙個總值最大的子陣列。例如a的乙個最大子陣列為a[7..10]=。
求解思想:
1)暴力求解
迴圈每乙個元素,並從每乙個元素的下標開始累加,每次選取較大的和。例如從第乙個元素開始,第一次累加是13,那麼記錄最大的maxsubarray為13;第二次累加為13-2=11,比13小,maxsubarray還是為13;然後繼續累加,選擇最大的maxsubarray;最後迴圈下乙個元素。a為輸入陣列,n為最大子陣列起始元素下標,m為最大子陣列終止元素下標,maxsum為最大子陣列的和
偽**:
findmaxsubarray(a)
maxsum=0
for i = 1 to a.length
let cursum = 0
for j = i to a.length
cursum = cursum + a[j]
if cursum > maxsum
maxsum = cursum
let n = i,m = j
return n,m,maxsum
2)分治思想
分治思想,把乙個問題分解為很多小問題,先解決小問題然後再逐級向上解決整個問題。該問題的解決思想跟歸併排序類似,先把陣列拆分為兩個子串行,然後兩個子串行繼續拆分。同樣也需要乙個輔助函式,用於計算子串行陣列對應下標的最大子陣列和。
偽**:
findmaxcrossingsubarray(a,low,mid,high) //輔助函式
left-sum = -∞
sum = 0
for i = mid down to low
sum = sum + a[i]
if sum > left-sum
left-sum = sum
max-left = i
right-sum = -∞
sum = 0
for j = mid + 1 to high
sum = sum + a[j]
if sum > righ-sum
right-sum = sum
max-right = j
return (max-left,max-right,left-sum+right-sum)
findmaxsubarrau(a, low, high) //主函式
if high == low
return (low,high,a[low]) //分解為只有乙個元素時
else
mid = (low+high)/2
(left-low,left-high,left-sum) = findmaxsubarrau(a,low,mid)
(right-low,right-high,right-sum) = findmaxsubarrau(a,mid+1,high)
(cross-low,cross-high,cross-sum) = findmaxcrossingsubarray(a,low,mid,high)
if left-sum >= right-sum and left-sum >= cross-sum
return (left-low,left-high,left-sum)
else if right-sum >= left-sum and right-sum >= cross-sum
return (right-low,right-high,right-sum)
else
return (cross-low,cross-high,cross-sum)
3)最優解
假如有這樣乙個陣列,陣列元素大於2並且陣列元素裡面有正負,那麼最大子陣列起始元素一定是正整數,每次從正整數開始計算,求出最大的子陣列。i為最大子陣列起始元素下標,j為最大子陣列終止元素下標,maxsum為最大字陣列的和
偽**:
findmaxsubarray(a) //注釋的**功能為記錄最大子陣列起始元素和終止元素的下標
flag = maxsum = cursum = 0
//i = j = 0;
for m = 0 to a.length
cursum = cursum + a[m]
if cursum > maxsum
//if flag != i
// i = flag
//j = m
maxsum = cursum
if cursum < 0
cursum = 0
//if m < a.length-1
// if a[m+1] > 0
// flag = m + 1
//return i,j,maxsum
return maxsum
class findmaxsubarray;//int a = ;
//存放最大字陣列的起始下標,終止下標,陣列和
int result = new int[3];
//result = findmaxsubarray1(a,result);
//result = findmaxsubarray2(a,result,0,a.length-1);
result = findmaxsubarray3(a,result);
for (int i: result)
} //暴力求解
public static int findmaxsubarray1(int a,int result)
}} getresult(result,i,j,maxsum);
return result;
} //分治思想
public static int findmaxsubarray2(int a,int result,int low,int high)else
}else if (rightresult[2]>=leftresult[2]&&rightresult[2]>=crossresult[2])
}else
}return result;
} }//分治過程中用到的輔助函式
public static int findmaxcrossingsubarray(int a,int result,int low,int mid,int high)
} int rightsum = a[mid+1];
int currightsum = 0;
int right = mid+1;
for(int i = mid+1; i <= high; i++)
} result = getresult(result,left,right,leftsum+rightsum);
return result;
} //最優解
public static int findmaxsubarray3(int a,int result)
j = m;//更換當前終止元素下標
maxsum = cursum;
//更換上一次最大子陣列終止元素的下標
/*if(k!=0||k!=j)
if ((j-k)!=1||j==0)*/
}if(cursum<0)}}
} result = getresult(result,i,j,maxsum);
return result;
} public static int getresult(int result,int i,int j,int maxsum)
}
求陣列的最大子陣列和最大子陣列的和
輸入乙個整形陣列,陣列裡有正數也有負數。陣列中連續的乙個或多個整數組成乙個子陣列,每個子陣列都有乙個和。求所有子陣列的和的最大值。要求時間複雜度為o n 例如輸入的陣列為1,2,3,10,4,7,2,5,和最大的子陣列為3,10,4,7,2,因此輸出為該子陣列的和18。include using n...
求陣列中最大子陣列的和
本篇部落格介紹第二週課上小測 求陣列中最大子陣列的和,使用c 實現。1 include2 using namespace std 3int main 419 int ans 100000 20 再次遍歷,最大值即為最大子陣列的和 21for i 1 i n i 22 ans max ans,a i ...
求陣列最大子串行的和
題目 給出陣列,求子序列的最大和。分別用一下兩種方法解決。include 方法1 分治法 時間複雜度 o nlogn int max3 int num1 int num2 int num3 int maxsubsum int array,int start,int end middle start ...