标签:
一、题目要求
#include<iostream> #include<time.h> #include<conio.h> #include<fstream> using namespace std; ofstream out; void RandIn(int RowIntNum,int ColIntNum,int A[][100])//随机生成一个二维数组 { for(int i=0;i<RowIntNum;i++) { for(int j=0;j<ColIntNum;j++) { A[i][j]=-(int)rand()%201+100; cout<<A[i][j]<<‘\t‘; } cout<<endl; } } int MaxArray(int array[],int Maxsize,int &Location) { int max=array[0];//生成最大数值 Location=0;//最大数值下标位置初始化 for(int i=0;i<Maxsize;i++) { if(max<array[i]) { max=array[i]; Location=i; } } return max; } void DivANum(int A[][100],int RowIntNum,int ColIntNum,int SubRowNum,int SubColNum,int EveTSum[],int &Location,int Times) { int sum[10000];//记录子矩阵的和 int ArrayNum=0;//生成子矩阵的数目 int count=0;//子矩阵累加次数 for(int row=0;row<RowIntNum-SubRowNum+1;row++) { for(int col=0;col<ColIntNum;col++)//求所有子矩阵 { sum[ArrayNum]=0; for(int p=row;p<row+SubRowNum;p++)//求得子矩阵的和 { for(int q=col;q<col+SubColNum;q++) { sum[ArrayNum]+=A[p][q%ColIntNum]; } } ArrayNum++;//记录生成的子矩阵数 } } EveTSum[Times]=MaxArray(sum,ArrayNum,Location);//记录每次生成的子矩阵中的最大子矩阵数值 } void Print(int row,int col, int array[][100]) { for(int i=0;i<row;i++) { for(int j=0;j<col;j++) { out<<array[i][j]<<‘,‘; } out<<‘\n‘; } } void main() { out.open("text.txt"); srand((unsigned)time(NULL)); int Location[100];//记录每次最大子数组的位置 int A[100][100],RowIntNum,ColIntNum; int SubRowNum,SubColNum; int max=0,LocTimes=0; int EveTSum[10000]; int q=0; while(q==0) { cout<<"请输入行数:"; cin>>RowIntNum; out<<RowIntNum<<‘,‘; cout<<"请输入列数:"; cin>>ColIntNum; out<<ColIntNum<<‘,‘<<‘\n‘; cout<<"整数内容"<<endl; RandIn(RowIntNum,ColIntNum,A);//随机生成RowIntNum行,ColIntNum列的数组 Print(RowIntNum,ColIntNum,A); for(SubRowNum=1;SubRowNum<=RowIntNum;SubRowNum++) { for(SubColNum=1;SubColNum<=ColIntNum;SubColNum++) { DivANum(A,RowIntNum,ColIntNum,SubRowNum,SubColNum,EveTSum,Location[(SubRowNum-1)*RowIntNum+SubColNum-1],(SubRowNum-1)*RowIntNum+SubColNum-1); } } cout<<"输出最大矩阵的和"<<endl; max=MaxArray(EveTSum,(SubRowNum-1)*(SubColNum-1),LocTimes); Location[LocTimes]++; cout<<max<<endl; out<<max; out.close(); cout<<"是否继续测试(输入0继续)"<<endl; cin>>q; system("cls"); system("pause"); } }
3月31号周二课堂练习:结对开发----求二维数组组成的矩阵中子矩阵的最大值二
标签:
原文地址:http://www.cnblogs.com/huangxiaofei/p/4384698.html