AddPeriodic
TCO19 SRM 761 · 2019-06-21 · by misof
Problem Statement
You are given two
- X, Y, Z are sequences of digits
- X and Z are non-empty
- X is either "0" or it starts with a nonzero digit.
Y is called the pre-period and Z is called the period. For example, "0.(3)" and "0.33(333)" are two valid representations of the number 1/3, and "12.(0)" and "11.(9)" are two valid representations of the number twelve.
Let C = A + B.
Return a
Constraints
- A and B will each have between 5 and 12 characters, inclusive.
- A and B will each have the form from the problem statement.
"0.33(333)" "0.(66)" Returns: "1.(0)"
1/3 + 2/3 = 1
"2.41(5)" "5.36(22)" Returns: "7.(7)"
2.41555555... + 5.362222222... = 7.77777777...
"685.4(757)" "45.356(43)" Returns: "730.832(210119)"
"0.(101)" "0.(23)" Returns: "0.(333424)"
"0.0(999999)" "1.5(00000)" Returns: "1.6(0)"
The input may contain a number whose period consists of all nines, but in order to have a unique correct output the output is not allowed to contain such a period. This test case is the addition 0.1 + 1.5 = 1.6
Submissions are judged against all 106 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class AddPeriodic with a public method string add(string A, string B) · 106 test cases · 2 s / 256 MB per case