返回一个二维整数数组中最大子数组的和

#include<stdio.h>

int main()
{
  int a[2][2];//定义一个2*2的二维数组
  int max;
  int s;
  int count;
  int b[2][3];
  int i,j;
  printf("请输入二维数组: ");
  for( i=0;i<2;i++)
  {
    for( j=0;j<2;j++)
    {
    scanf("%d",&a[i][j]);
    }

  }
  for( i=0;i<2;i++)
    for( j=0;j<2;j++)
      printf("%d",&a[i][j]);

  for(i=0;i<2;i++)
  {
    count=0;
    for(int j=0;j<2;j++)
    {
     s=0;
     for(int l=0;l<2-j;l++)
     {
      s=s+a[i][j+l];
      b[i][count+l]=s;

     }
        count=count+2-j;
    }
  }
  int c[3][3];
  for(j=0;j<3;j++)
  {
    count=0;
    for(int i=0;i<2;i++)
    {
      s=0;
      for(int l=0;l<2-i;l++)
      {
        s=s+b[i+l][j];
        c[count+l][j]=s;

      }
      count=count+2-i;
    }
  }
  int d,e,f;

  if( c[0][0]>c[1][0]) d = c[0][0];
  else d = c[1][0];
  if( c[2][0]>d) d = c[2][0];

  if( c[0][1]>c[1][1]) e = c[0][1];
  else e = c[1][1];
  if( c[2][1]>d) e = c[2][1];

  if( c[0][2]>c[1][2]) f = c[0][2];
  else f = c[1][2];
  if( c[2][2]>d) f = c[2][2];


  if( d>e) max = d;
  else max = e;
  if( f>max) max = f;
  printf(" 最大子数组和为:");

  printf("%d ",max);

  return 0;
}

 

原文地址:https://www.cnblogs.com/mengweile/p/9826415.html