ShoelaceLength
SRM 213 · 2004-09-28 · by dplass
Problem Statement
My shoelace broke the other day. I was too lazy to take it out and figure out how long it was, so I decided to calculate the length based on the number of eyelets and their separation.
The "horizontal" distance between the eyelets can increase or decrease from the bottom to the top of the shoe. The startWidth is how far apart they are at the bottom (near the toes) and the endWidth is how far apart they are at the top (near the tongue of the shoe). This distance increases (or decreases) linearly from bottom to top. Furthermore, the "vertical" distance, spread, between eyelets is constant.
Write a method, calculate, which calculates the length of the entire shoelace, given the initial distance between the eyelets (startWidth), the final distance between the eyelets (endWidth), distance between each pair of vertically adjacent eyelets (spread) and the number of pairs of eyelets (numPairs).
You should assume all laces follow this general pattern:
|----| <- startWidth
O----O -
\ / |
\/ | <- spread
/\ |
/ \ |
O O -
\ /
\/
/\
/ \
O O
|----| <- endWidth
In this illustration, startWidth and endWidth are identical, and numPairs is 3.
Notes
- startWidth may be greater than, equal to, or less than endWidth.
- Your return value must have a relative or absolute error less than 1e-9.
- The units of measurement are irrelevant.
- The length of lace that it takes to go around or through each eyelet should be ignored.
- The length of lace needed to actually tie a knot is not included.
Constraints
- numPairs is the number of PAIRS of eyelets, and will be between 2 and 50, inclusive.
- startWidth, endWidth, and spread will each be between 1 and 99, inclusive.
2 2 1 2 Returns: 6.47213595499958
These are my topsiders. Remember to add the start width across the bottom pair of eyelets.
10 1 1 10 Returns: 111.1786186482248
1 10 1 10 Returns: 102.17861864822481
Nearly the same configuration as the previous case, except the width goes from 1 to 10 instead of 10 to 1.
1 1 1 2 Returns: 3.8284271247461903
1 99 19 2 Returns: 107.97663296253066
Submissions are judged against all 42 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class ShoelaceLength with a public method double calculate(int startWidth, int endWidth, int spread, int numPairs) · 42 test cases · 2 s / 256 MB per case