一、设计思路:

1.先定义一个2*2数组并输入。

2.再求每行中子数组的和,得到一个2*3数组。

3.求每列中子数组的和,最后得到一个3*3数组。

4.求每一列的最大值,得到3个数。

5.3个数比较得到一个最大值即最大子数组的和。

二、代码:

#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("请输入二维数组:\n");
  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("\n最大子数组和为:");

  printf("%d\n",max);

  return 0;
}

三、运行结果:

四、psp0

 

 

五、心得体会

    上课认真听讲,下课要多看书。

 

posted on 2018-10-21 14:14  GAOE  阅读(110)  评论(0编辑  收藏  举报