1 #include "stdafx.h" 2 #include <iostream> 3 #include <stdio.h> 4 #include <vector> 5 #include <assert.h> 6 7 using namespace std; 8 vector<int> vec; 9 10 int Char2Int(char *a, int n); 11 char LastChar(char *a, int n); 12 13 typedef struct tagMatrixInfo 14 { 15 int i32nLine; 16 int i32nCol; 17 18 tagMatrixInfo() 19 { 20 i32nLine = i32nCol = 0; 21 } 22 }S_MatrixInfo; 23 24 25 S_MatrixInfo GetMax2X2Matrix(int **ppMatrix, int nLine, int nCol) 26 { 27 assert(ppMatrix); 28 assert(nLine >= 2); 29 assert(nCol >= 2); 30 31 S_MatrixInfo sMaxMatrixInfo; 32 33 int nMaxValue = 0, nTempValue = 0; 34 for (int i32I = 0; i32I < nLine - 1; i32I++) 35 { 36 for (int i32J = 0; i32J < nCol - 1; i32J++) 37 { 38 nTempValue += (ppMatrix[i32I][i32J] + ppMatrix[i32I][i32J + 1]); 39 nTempValue += (ppMatrix[i32I + 1][i32J] + ppMatrix[i32I + 1][i32J + 1]); 40 41 if (nTempValue > nMaxValue) 42 { 43 nMaxValue = nTempValue; 44 sMaxMatrixInfo.i32nLine = i32I; 45 sMaxMatrixInfo.i32nCol = i32J; 46 } 47 nTempValue = 0; 48 } 49 } 50 51 return sMaxMatrixInfo; 52 } 53 54 int main(void) 55 { 56 57 int i32I = 0, i32J = 0; 58 int i32nLine = 0, i32nCol = 0; 59 char temp[10]; 60 char a; 61 int count = 0,temp1=0; 62 while (a=cin.get() != ‘\n‘) 63 { 64 cin.unget(); 65 cin >> temp; 66 67 if (LastChar(temp,10) == ‘;‘) 68 { 69 if (i32nCol == 0) 70 { 71 i32nCol = vec.size()+1; 72 } 73 i32nLine++; 74 } 75 vec.push_back(Char2Int(temp, 8)); 76 } 77 i32nLine++; 78 cout << i32nLine << "行," << i32nCol << "列。" << endl; 79 vector<int>::iterator it; 80 for (it = vec.begin(); it != vec.end(); it++) 81 { 82 count++; 83 if (count%i32nCol == 0) 84 { 85 cout << *it << ‘ ‘<<endl; 86 } 87 else 88 { 89 cout << *it << ‘ ‘; 90 } 91 92 } 93 94 int **ppMatrix = new int *[i32nLine]; 95 assert(ppMatrix); 96 97 for (i32I = 0; i32I < i32nLine; i32I++) 98 { 99 ppMatrix[i32I] = new int[i32nCol]; 100 assert(ppMatrix[i32I]); 101 102 for (i32J = 0; i32J < i32nCol; i32J++) 103 { 104 ppMatrix[i32I][i32J]=vec[i32I*i32nCol+i32J]; 105 } 106 } 107 108 S_MatrixInfo sMaxMatrixInfo = GetMax2X2Matrix(ppMatrix, i32nLine, i32nCol); 109 cout << endl << "最大的二维矩阵为:" << endl; 110 for (i32I = sMaxMatrixInfo.i32nLine; i32I < sMaxMatrixInfo.i32nLine + 2; i32I++) 111 { 112 for (i32J = sMaxMatrixInfo.i32nCol; i32J < sMaxMatrixInfo.i32nCol + 2; i32J++) 113 { 114 cout << ppMatrix[i32I][i32J] << " "; 115 } 116 cout << endl; 117 } 118 119 return 0; 120 121 122 } 123 124 int Char2Int(char *a, int n) 125 { 126 if (a == NULL) 127 { 128 return 0; 129 } 130 int i = 0; 131 int result = 0; 132 while (a[i] != NULL) 133 { 134 if (a[i] != ‘;‘) 135 { 136 result *= 10; 137 result += a[i] - ‘0‘; 138 139 } 140 i++; 141 142 143 } 144 return result; 145 146 } 147 148 char LastChar(char *a, int n) 149 { 150 int i = 0; 151 int j = 0; 152 while (a[i] != NULL) 153 { 154 j = i; 155 i++; 156 } 157 158 return a[j]; 159 }
原文:http://www.cnblogs.com/hhboboy/p/4844524.html