zoukankan      html  css  js  c++  java
  • 返回一个二维整数数组中最大联通子数组的和

    题目:返回一个二维整数数组中最大联通子数组的和

    要求:

             输入一个二维整形数组,数组里有正数也有负数。

             求所有子数组的和的最大值    

    代码:

    #include<iostream>

    #include<stdlib.h>

    #include<iomanip>

    using namespace std;

    #define Max 100

    void main () {  

    int n=5,m=5;  

    int array[5][5];  

    {

    for(int j=0;j<m;j++)   

    {    

    array[i][j]=-Max+rand()%(Max*2+1);   

    cout<<setw(5)<<array[i][j];   

    }   cout<<endl;  

    }  

    int sum=array[0][0],temp=0;    

    for(int i=0;i<n;i++)  

    for(int j=0;j<m;j++)   

    {      int k=i,l=j;          

    while(1!=0)             

      {          

    while(l<m)             

    {                                   

    if(k>=n)                    

    {                          

    for(int a=i;a<k;a++)                     

    {                           

    for(int b=j;b<=l;b++)                           

    {                             

    temp=temp+array[a][b];         

     }                 

     }                            

    for(int a=0;a<=(k%n);a++)                    

    {                     

    for(int b=i;b<=l;b++)                        

    {                           

    temp=temp+array[a][b];        

      }         

    }        

    }        

    else        

    {        

    for(int a=i;a<=k;a++)                     

    {                           

    for(int b=j;b<=l;b++)                           

    {                             

    temp=temp+array[a][b];        

      }                    

    }        

    }                          

    if(temp>=sum)                      

    {                          

    sum=temp;       

    }             

    temp=0;             

    l++;      

    }         

    k++;            

    l=j ;                  

    if(k%n==i)      

    break;     

    }        

    }    

    }    

    cout<<sum<<endl;   }

    截图:

  • 相关阅读:
    什么叫TLD、gTLD、nTLD、ccTLD、iTLD 以及几者之间的关系
    socket
    windows下codeblocks报错undefined reference to `WSAStartup@8'|
    Codeforces 467C George and Job | DP
    51Nod 1049最大子段和 | 模板
    51Nod 最大子矩阵和 | DP
    AtomicInteger
    sun.misc.unsafe
    CAS
    java中的四种引用
  • 原文地址:https://www.cnblogs.com/mingning/p/4537652.html
Copyright © 2011-2022 走看看