TopcoderArchive
TCO21 Jun 2019
TCO19 SRM 761

AddPeriodic

Problem statement, definition, constraints, and public examples.

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.

Definition

Class:
AddPeriodic
Method:
add
Parameters:
String, String
Returns:
String
Method signature:
String add(String A, String B)
(be sure your method is public)

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

  1. "0.33(333)"
    "0.(66)"
    Returns: "1.(0)"
    1/3 + 2/3 = 1
  2. "2.41(5)"
    "5.36(22)"
    Returns: "7.(7)"
    2.41555555... + 5.362222222... = 7.77777777...
  3. "685.4(757)"
    "45.356(43)"
    Returns: "730.832(210119)"
  4. "0.(101)"
    "0.(23)"
    Returns: "0.(333424)"
  5. "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
Back to all problems