/* TriangleMaking (SRM 697, D2, 250 from Topcoder). James S. Plank Sun Aug 28 14:33:41 EDT 2016 */ #include #include #include #include #include #include using namespace std; class TriangleMaking { public: int maxPerimeter(int a, int b, int c); }; int TriangleMaking::maxPerimeter(int a, int b, int c) { /* As I said in the writeup, my solution pushes the three values onto a vector and then sorts the vector. You have to include when you do this, or you'll have problems compiling the program. */ vector ABC; ABC.push_back(a); ABC.push_back(b); ABC.push_back(c); sort(ABC.begin(), ABC.end()); /* Now, the longest side is ABC[2], and the two shortest sides are ABC[0] and ABC[1]. You test to see if the sum of the shorter two sides is less than or equal to the longest side. If so, you shorten the longest side so that it is the sum of the other two, minus one: */ if (ABC[0] + ABC[1] <= ABC[2]) ABC[2] = ABC[0] + ABC[1] - 1; /* Return their perimeter: */ return ABC[0] + ABC[1] + ABC[2]; }