Project Euler Homework
|
00001 00031 #include <iostream> 00032 #include <fstream> 00033 using namespace std; 00034 00035 int 00036 main () 00037 { 00038 ifstream input; 00039 input.open ("011.txt"); 00040 00041 unsigned short N[20][20]; 00042 for (unsigned short *p = &(N[0][0]); !input.eof (); p++) 00043 input >> *p; 00044 00045 unsigned max_product = 0; 00046 int x1, y1, x2, y2; 00047 00048 //Search in rows 00049 for (int i = 0; i < 20; i++) 00050 for (int j = 0; j < 20 - 3; j++) 00051 { 00052 unsigned product = 00053 unsigned (N[i][j]) * N[i][j + 1] * N[i][j + 2] * N[i][j + 3]; 00054 if (product > max_product) 00055 { 00056 max_product = product; 00057 x1 = i; 00058 y1 = j; 00059 x1 = i; 00060 y2 = j + 3; 00061 } 00062 } 00063 00064 //Search in columns 00065 for (int j = 0; j < 20; j++) 00066 for (int i = 0; i < 20 - 3; i++) 00067 { 00068 00069 unsigned product = 00070 unsigned (N[i][j]) * N[i + 1][j] * N[i + 2][j] * N[i + 3][j]; 00071 00072 if (product > max_product) 00073 { 00074 max_product = product; 00075 x1 = i; 00076 y1 = j; 00077 x2 = i + 3; 00078 y2 = j; 00079 } 00080 } 00081 00082 //Search from top-left to bottom-right 00083 for (int i = 0; i < 20 - 3; i++) 00084 for (int j = 0; j < 20 - 3; j++) 00085 { 00086 00087 unsigned product = 00088 unsigned (N[i][j]) * N[i + 1][j + 1] * N[i + 2][j + 2] * N[i + 00089 3][j + 00090 3]; 00091 00092 if (product > max_product) 00093 { 00094 max_product = product; 00095 x1 = i; 00096 y1 = j; 00097 x2 = i + 3; 00098 y2 = j + 3; 00099 } 00100 } 00101 00102 //Search from top-right to bottom-left 00103 for (int i = 0; i < 20 - 3; i++) 00104 for (int j = 3; j < 20; j++) 00105 { 00106 00107 unsigned product = 00108 unsigned (N[i][j]) * N[i + 1][j - 1] * N[i + 2][j - 2] * N[i + 00109 3][j - 00110 3]; 00111 00112 if (product > max_product) 00113 { 00114 max_product = product; 00115 x1 = i; 00116 y1 = j; 00117 x2 = i + 3; 00118 y2 = j - 3; 00119 } 00120 } 00121 cout << max_product << endl << " from row " << (x1 + 1) << ", column " 00122 << (y1 + 1) << " to row " << (x2 + 1) << ", column " << (y2 + 1) << endl; 00123 return 0; 00124 } 00125