Connection Status:
Competition Arena > Abacus
SRM 292 · 2006-03-06 · by dgoodman · Simple Math, String Manipulation
Class Name: Abacus
Return Type: String[]
Method Name: add
Arg Types: (vector<string>, int)
Problem Statement

Problem Statement

An abacus can be used to do arithmetic. The version that we have has 6 horizontal threads, each with nine beads on it. The beads on each thread are always arranged with just one gap, possibly at one of the ends. However many beads are adjacent and at the right end of the thread is the digit value of the thread. The value on the abacus is read by taking the digits in order from top thread to bottom thread and arranging them from left to right (so the top thread is the one that contains the most significant digit).

Create a class Abacus that contains a method add that is given a String[] original and a number val and that returns a String[] showing the abacus after val has been added to the original abacus.

Both in original and in the return, the String[] will contain exactly 6 elements representing the 6 threads in order from top thread to bottom thread. Each element will contain a lowercase 'o' to represent each bead and three consecutive hyphens '-' to indicate the empty part of the thread. Each element will thus contain exactly 12 characters.

Constraints

  • original will contain exactly 6 elements.
  • Each element of original will contain exactly 12 characters, 9 lowercase 'o's and 3 consecutive '-'s.
  • val will be between 0 and 999,999 inclusive.
  • val added to the original abacus will result in a value that can be shown on the abacus.
Examples
0)
{"ooo---oooooo",
 "---ooooooooo",
 "---ooooooooo",
 "---ooooooooo",
 "oo---ooooooo",
 "---ooooooooo"}
5
Returns: {"ooo---oooooo", "---ooooooooo", "---ooooooooo", "---ooooooooo", "o---oooooooo", "ooooo---oooo" }

When we add 5 to the original, it is necessary to "carry" 1 to the next thread up. This shows the arithmetic 699979 + 5 = 699984

1)
{"ooo---oooooo",
 "---ooooooooo",
 "---ooooooooo",
 "---ooooooooo",
 "oo---ooooooo",
 "---ooooooooo"}
21
Returns: {"oo---ooooooo", "ooooooooo---", "ooooooooo---", "ooooooooo---", "ooooooooo---", "ooooooooo---" }

This shows 699979 + 21 = 700000

2)
{"ooo---oooooo",
 "---ooooooooo",
 "---ooooooooo",
 "---ooooooooo",
 "ooooo---oooo",
 "---ooooooooo"}
7123
Returns: {"oo---ooooooo", "ooooooooo---", "oo---ooooooo", "ooooooooo---", "oo---ooooooo", "ooooooo---oo" }
3)
{"ooo---oooooo",
 "---ooooooooo",
 "---ooooooooo",
 "---ooooooooo",
 "oo---ooooooo",
 "---ooooooooo"}
99
Returns: {"oo---ooooooo", "ooooooooo---", "ooooooooo---", "ooooooooo---", "oo---ooooooo", "o---oooooooo" }
4)
{"ooooooooo---",
 "---ooooooooo",
 "ooooooooo---",
 "---ooooooooo",
 "oo---ooooooo",
 "---ooooooooo"}
100000
Returns: {"oooooooo---o", "---ooooooooo", "ooooooooo---", "---ooooooooo", "oo---ooooooo", "---ooooooooo" }

Submissions are judged against all 61 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class Abacus with a public method vector<string> add(vector<string> original, int val) · 61 test cases · 2 s / 256 MB per case

Submitting as anonymous