Maximum triangle path sum: Difference between revisions
Content added Content deleted
(made more fluent) |
|||
Line 312: | Line 312: | ||
}; |
}; |
||
int |
const int size = sizeof( triangle ) / sizeof( int ); |
||
const int tn = static_cast<int>(sqrt(2.0 * size)); |
|||
tn = 1; |
|||
assert(tn * (tn + 1) == 2 * size); // size should be a triangular number |
|||
// walk backward by rows, replacing each element with max attainable therefrom |
|||
for (int n = tn - 1; n > 0; --n) // n is size of row, note we do not process last row |
|||
for (int k = (n * (n-1)) / 2; k < (n * (n+2)) / 2; ++k) |
|||
⚫ | |||
last--; |
|||
for( int n = tn; n >= 2; n-- ) |
|||
{ |
|||
for( int i = 2; i <= n; i++ ) |
|||
{ |
|||
⚫ | |||
last--; |
|||
} |
|||
last--; |
|||
} |
|||
std::cout << "Maximum total: " << triangle[0] << "\n\n"; |
std::cout << "Maximum total: " << triangle[0] << "\n\n"; |
||
return system( "pause" ); |
|||
} |
} |
||
</lang> |
</lang> |