TopCoder problem "CalculatorDisplay" used in TCHS07 Delta 2 (Division I Level Three)



Problem Statement

    

You have a calculator with a screen that can display up to digits digits along with a single decimal point. If the result of an operation is a number that cannot fit on the screen, the calculator will show just "E" (quotes for clarity) rather than the actual result. Note that fractional numbers less than 1.0 are always shown without the leftmost 0 (for example, ".125"). Also note that integer numbers are never shown with a decimal point, so 4 would always be shown as "4", not "4." or "4.0"." Leading zeros are never displayed (at the left of integer part or the right in the fractional part).

Given a numerator n and denominator d, return the content of the calculator's screen after dividing n by d. For example, 10005 divided by 400 is 25.0125. A 6-digit display would show "25.0125" while a 5-digit display would show "E" (all quotes for clarity only).

 

Definition

    
Class:CalculatorDisplay
Method:numberDivision
Parameters:int, int, int
Returns:String
Method signature:String numberDivision(int digits, int n, int d)
(be sure your method is public)
    
 

Constraints

-digits will be between 1 and 20, inclusive.
-n will be between 0 and 1,000,000,000, inclusive.
-d will be between 1 and 1,000,000,000, inclusive.
 

Examples

0)
    
10
10005
400
Returns: "25.0125"
The example from the problem statement.
1)
    
5
10005
400
Returns: "E"
The same result would not fit in only five digits.
2)
    
10
1
8
Returns: ".125"
3)
    
5
434544
352
Returns: "1234.5"
Note that the decimal point does not add a position, and the result occupies all of its five digits.
4)
    
20
1
3
Returns: "E"
To display the result would require an infinite number of fractional digits!
5)
    
13
0
987654321
Returns: "0"
The result is just zero. Note that the decimal point is not shown if the result is an integer.

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=6458

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10714&pm=6458

Writer:

ged

Testers:

PabloGilberto , brett1479 , Olexiy

Problem categories:

Simple Math