CutoffRounder
SRM 189 · 2004-03-31 · by lars2520
SRM 189 · 2004-03-31 · by lars2520 · Math
Problem Statement
Problem Statement
Often, when we round a real valued number to an integer, we round up if the fractional part is 0.5 or greater, and down if the fractional part is less than 0.5. In this problem, you are to write a method round, which takes a real valued number as a String , num, and a cutoff as a String , cutoff. cutoff will be formatted exactly as "0.####", where each '#' represents a digit ('0'-'9'). At least one of the digits to the right of the decimal point in cutoff will be non-zero. Your task is to round num up if its fractional part is greater than cutoff, and down otherwise, and return the result as an int . To avoid issues with double imprecision, the fractional part of num will not be exactly equal to cutoff
Hence, the traditional rounding method described in the opening sentence would be represented by cutoff = "0.5000"
Hence, the traditional rounding method described in the opening sentence would be represented by cutoff = "0.5000"
Constraints
- cutoff will be formatted exactly as "0.####", where each '#' represents a digit ('0'-'9').
- num will be a sequence of one or more digits ('0'-'9'), with an optional decimal point ('.').
- num will contain between 1 and 10 characters, inclusive.
- The fractional part of num will not be exactly equal to cutoff.
- num will be between 0 and 2,000,000,000, inclusive.
Examples
0)
"003.656930" "0.5000" Returns: 4
0.65693 is greater than 0.5000, so we round up.
1)
".001" "0.0001" Returns: 1
A very low cutoff.
2)
"1.99999999" "0.9999" Returns: 2
3)
"135" "0.6531" Returns: 135
4)
"135." "0.6531" Returns: 135
Submissions are judged against all 45 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Coding Area
Language: C++17 · define a public class CutoffRounder with a public method int round(string num, string cutoff) · 45 test cases · 2 s / 256 MB per case