You are given three integers ‘X’, ‘Y’ and ‘N’. Find the value of division X / Y up to ‘N’ decimal places.
You should return a string that represents the value X / Y up to ‘N’ decimal places. This string must have ‘N’ digits after decimals. You don’t need to round off the result to ‘N’ decimal places, just find the first ‘N’ places after decimals.
Note:
1. It is guaranteed that division X / Y is always finite.
Example:
Consider X = 5, Y = 4 and N = 5, then you should return “1.25000”.
Note, here we add 3 trailing zeros so that it has exactly 5 digits after decimals.
The first line of input contains an integer ‘T’ denoting the number of test cases, then ‘T’ test cases follow.
The first and only line of each test case consists of three space-separated integers, ‘X’, ‘Y’, ‘N’ respectively.
Output format :
For each test case, print in a separate line, a string representing the value X / Y up to ‘N’ decimal places, and it must have ‘N’ digits after decimals.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 50
-10^8 <= X <= 10^8
-10^8 <= Y <= 10^8
Y != 0
1 <= N <= 10^4
Time limit: 1 sec
2
5 4 1
5 4 5
1.2
1.25000
Test case 1:
Division 5/4 = 1.25, but we need to print only ‘1’ digit after decimals, thus we should print 1.2.
Test case 2:
See the problem statement for an explanation.
3
-1 -2 1
1 -5 6
-1 1000 2
0.5
-0.200000
-0.00
Repeatedly multiply the remainder by 10 and divide it with the given dividend to find digits after the decimal.
Following is the detailed algorithm :
O(N), where ‘N’ is the number of digits after the decimal.
We are iterating on the number of digits after decimal. Thus the time complexity will be O(N).
O(N), where ‘N’ is the number of digits after the decimal.
The length of the string ‘RESULT’ will be of order O(N). Thus, overall space complexity will be O(N).