1. 程式人生 > >返回一個二維整數組中最大子數組的和

返回一個二維整數組中最大子數組的和

輸入 info 依次 count cin src ret ++ 最大子數組

題目:返回一個二維整數組中最大子數組的和

實驗思路

根據老師上課給出的3*6的數據 我們決定設計一個3*6行的二維數組進行計算,依次進行比較 將最大子數組的和返回

代碼

 1 #include <iostream.h>
 2 int main()
 3 {
 4     int a[3][6];//定義一個3*6的二維數組
 5     int max;
 6     int s;//求和
 7     int count;
 8     int b[3][7];
 9     cout<<"請輸入二維數組(3*6)中的元素:"<<endl;
10     for(int
i=0;i<3;i++) 11 { 12 for(int j=0;j<6;j++) 13 { 14 cin>>a[i][j]; 15 } 16 } 17 for(i=0;i<3;i++) 18 { 19 count=0; 20 for(int j=0;j<6;j++) 21 { 22 s=0; 23 for(int l=0;l<6-j;l++) 24 {
25 s=s+a[i][j+l]; 26 b[i][count+l]=s; 27 } 28 count=count+6-j; 29 } 30 } 31 //求最大數 32 max=b[0][0]; 33 for(int j=0;j<7;j++) 34 { 35 for(i=0;i<3;i++) 36 { 37 s=0; 38 for(int r=0;r<3
-i;r++) 39 { 40 s=s+b[r+i][j]; 41 if(max<s) 42 { 43 max=s; 44 } 45 } 46 } 47 } 48 cout<<"最大子數組為:"<<max<<endl; 49 cout<<endl; 50 return 0; 51 }

結果截圖

技術分享圖片

技術分享圖片

二人結對:王馳 張賓

技術分享圖片

返回一個二維整數組中最大子數組的和