Connection Status:
Competition Arena > AddPeriodic
TCO19 SRM 761 · 2019-06-21 · by misof · Greedy, Math, Simple Search, Iteration
Class Name: AddPeriodic
Return Type: String
Method Name: add
Arg Types: (string, string)
Problem Statement

Problem Statement

You are given two Strings A and B, each containing the representation of a nonnegative periodic decimal. More precisely, A and B are each given in the form "X.Y(Z)", where:

  • 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 String containing a representation of C in the same format. Minimize the length of the pre-period and also the length of the period in the representation of C. If the period can be either 0 or 9, use 0.

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.
Examples
0)
"0.33(333)"
"0.(66)"
Returns: "1.(0)"

1/3 + 2/3 = 1

1)
"2.41(5)"
"5.36(22)"
Returns: "7.(7)"

2.41555555... + 5.362222222... = 7.77777777...

2)
"685.4(757)"
"45.356(43)"
Returns: "730.832(210119)"
3)
"0.(101)"
"0.(23)"
Returns: "0.(333424)"
4)
"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.

Coding Area

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

Submitting as anonymous