/*======================================================================
# Author: TianYongTao&&ZhangYaPeng
# E-Mail: 714251829@qq.com
# Last modified: 2015-03-25 22:13
# Filename: Demo.cpp
# Description:
======================================================================*/
# include <iostream>
# include <stdio.h>
# include <fstream>
# include <string>
# define MaxRow 20
# define MaxCol 20
using namespace std;
int Arr[MaxRow][MaxCol];
int SumArr(int x1,int y1,int x2,int y2) //子数组求和
{
int sum=0;
for(int i=x1;i<x2;i++)
{
for(int j=y1;j<y2;j++)
{
sum+=Arr[i][j];
}
}
return sum;
}
void LoadFile(int Arr[][MaxCol],int & Row,int & Col) //读取文件到数组
{
FILE * infile = fopen("D:\\input.txt","r");
int num[100];
int count=0;
if(!infile)
{
cout<<"文件读取失败!"<<endl;
exit(-1);
}
else
{
char str[10];
fscanf(infile,"%[^,]%*c",str); //使用正则表达式过滤掉','
Row = atof(str);
fscanf(infile,"%[^,]%*c",str);
Col = atof(str);
while(!feof(infile)) //此时读取的是数组的内容 先将其放入一个整型数组
{
fscanf(infile,"%[^,]%*c",str);
num[count++]=atof(str);
}
}
for(int i=Row-1;i>=0;i--) //将整型数组内容放入Arr数组中
{
for(int j=Col-1;j>=0;j--)
{
Arr[i][j]=num[--count];
}
}
}
//测试函数
int main()
{
int Row=0;
int Col=0;
LoadFile(Arr,Row,Col);
int x1,y1; //代表左上角点的横纵坐标
int x2,y2; //代表右上角点的横纵坐标
int max = Arr[0][0]; //max存储比较过程中的最大值
for(x1=0;x1<Row;x1++)
{
for(y1=0;y1<Col;y1++)
{
for(x2=x1+1;x2<=Row;x2++)
{
for(y2=y1+1;y2<=Col;y2++)
{
if(SumArr(x1,y1,x2,y2)>max)
{
max = SumArr(x1,y1,x2,y2);
}
}
}
}
}
cout<<"该数组的最大子数组的和为:"<<max<<endl;
return 0;
}