TriangleMaking
SRM 697 · 2016-07-09 · by Arterm
SRM 697 · 2016-07-09 · by Arterm · Simple Math
Problem Statement
Problem Statement
You have three sticks.
Their current lengths are a, b, and c.
You can shorten each of those sticks arbitrarily.
Your goal is to produce three sticks with the following properties:
int s a, b, and c.
Compute and return the largest possible perimeter of the triangle constructed from your three sticks.
- The length of each stick is a positive integer.
- The three sticks can be used to build a triangle. The triangle must be non-degenerate. (I.e., it must have a positive area.)
- The perimeter of the triangle must be as large as possible.
Notes
- The return value is always defined. In other words, for any a, b, and c there is at least one way to build a valid triangle.
Constraints
- a will be between 1 and 100, inclusive.
- b will be between 1 and 100, inclusive.
- c will be between 1 and 100, inclusive.
Examples
0)
1 2 3 Returns: 5
Shorten the last stick from 3 to 2. There is a triangle with sides (1,2,2). The perimeter of this triangle is 1+2+2 = 5.
1)
2 2 2 Returns: 6
Here the optimal solution is to use the three sticks you have without shortening any of them.
2)
1 100 1 Returns: 3
3)
41 64 16 Returns: 113
4)
7 35 12 Returns: 37
Submissions are judged against all 40 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Coding Area
Language: C++17 · define a public class TriangleMaking with a public method int maxPerimeter(int a, int b, int c) · 40 test cases · 2 s / 256 MB per case