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

來源:互聯網
上載者:User

標籤:gen   div   amp   程式   system   就會   ann   turn   ext   

  程式設計思路:首先若要對二維數組進行分析,通常想要把它化簡成為一個一維數組。再先求每個一維數組的最大子數組和,並記下每行最大一維子數組的下標。這是就會分兩種情況:第一種是行之間的最大子數組是相連的,這時就可以直接相加得到;第二種是不相連的,,這時候就把每行的最大子數組看成一個整體,再使每個最大數組塊進行相連,求使其相連的最小代價。最後得到的就是最大聯通子數組的和。

  程式原始碼:

  1 package 最大子數組;  2   3 import java.util.Scanner;  4   5 public class judge2 {  6   7     public static void main(String[] args) {  8         // TODO Auto-generated method stub  9  10  11         int m,n,i,j,t2; 12         Integer smark=new Integer(0); 13         Integer mmark=new Integer(0); 14  15         int sum; 16  17         int up[]=new int[100]; 18         int down[]=new int[100]; 19         int t[]=new int[100]; 20         int a[][]=new int[100][100]; 21         int b[]=new int[100]; 22              23         System.out.println("請輸入二維數組的行數和列數:"); 24          Scanner scanner=new Scanner(System.in); 25         m=scanner.nextInt(); 26         n=scanner.nextInt(); 27         System.out.println("請輸入這個二維矩陣:"); 28          29         for(i=0;i<m;i++) 30  31         { 32  33             for(j=0;j<n;j++) 34  35             { 36  37                 a[i][j]=scanner.nextInt();; 38  39             } 40  41         } 42  43         //把二維數組按行分解為幾個一維數組 44         for(i=0;i<m;i++) 45  46         { 47  48             for(j=0;j<n;j++) 49  50             { 51  52                 b[j]=a[i][j]; 53  54             } 55  56             sum=Max(n,b,smark,mmark); 57  58             up[i]=smark;                                     59  60             down[i]=mmark; 61  62             t[i]=sum; 63  64       65  66         } 67  68         t2=t[0]; 69  70         for(i=0;i+1<m;i++) 71  72         { 73  74             if(up[i]<=down[i+1] && down[i]>=up[i+1]) 75  76             { 77  78                 t2+=t[i+1]; 79  80             } 81  82              for(j=up[i];j<up[i+1];j++) 83  84             { 85  86                 if(a[i+1][j]>0) t2+=a[i+1][j];                   //判別獨立正數 87  88             } 89  90       91  92         } 93  94         System.out.println(t2);    95     } 96     public static int Max(int n,int a[],Integer smark,Integer mmark) 97  98     { 99 100         int b[]=new int [100];101 102         int i,sum1= Integer.MIN_VALUE,max1=0;103         int j=0;104         for(i=0;i<n+j;i++)105 106         {107 108             if(sum1<0)109 110             {111 112                 sum1=a[i%n];113                 j=i;114             }115 116             else117 118             {119 120                 sum1=sum1+a[i%n];121 122             }123            124             b[i]=sum1;125 126         }127 128         max1=b[0];129       130         for(i=0;i<n+j;i++)131 132         {133 134             if (max1<b[i]) 135 136              {137 138                  max1= b[i];139 140                  mmark = i;141                 142 143              }144             145         }146 147          for (i = mmark;i >= 0;i--) 148 149         {150 151             if (b[i] == a[i]) 152 153             {154 155                  smark = i;156 157                  break;158 159             }160 161         }162 163          return max1;164 165     }166 }167         

 

  程式運行結果:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.