Access:

» TopCoder Problem: Abacus

Related categories: C/C++ | Tutorials | Programming in generall

dgoodman
Viewed: 1946 | Article date: 2006-07-18 15:04:19

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.

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.

About the author

dgoodman is a longstanding TopCoder member with a ‘yellow’ rating of 1539.

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.

Definition

  • Class: Abacus

  • Method: add

  • Parameters: String[], int

  • Returns: String[]

  • Method signature: String[] add(String[] original, int val)

(be sure your method is public)

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

Example 0) 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

 
{"ooo---oooooo",
"---ooooooooo",
"---ooooooooo",
"---ooooooooo",
"oo---ooooooo",
"---ooooooooo"}
5
Returns:
{"ooo---oooooo",
"---ooooooooo",
"---ooooooooo",
"---ooooooooo",
"o---oooooooo",
"ooooo---oooo" }

A d v e r t i s e m e n t
Linux BSD Unix ranking vote

Page: 1 2
Buy article Buy subscription
Buy now add to cart
add to cart
Standard price: 2€/$3 Standard price: 25€/$30
Buy article for as little as (2€/$3) each allow access to individual articles. Buy a full access to our Software Developers's Journal archive portal. You will be able to read the articles from all archive issues from year 2005 and 2006. For just 25€/$30 you get unrestricted access to the entire website for the whole year.
SDJhakin9

.SDJ Users:


.:Login
.:Password

[Register]
[Forgotten your password?]

...Shopping Cart

sum: 0 €
Choose currency:

...Topics

...Advertisement

www.acunetix.com www.verifysoft.com

...Conferences




...Print Edition Archive

...Affiliate Program



 

 

Subscribe | Contact Us | Newsletter | Privacy policy | Regulations | See all issues | About SDJ
Copyright C 2006 by Software Developer's Journal. All rights reserved.