Given a fraction. Convert it into a decimal. So simple :P
eg.
10/2 = 5
3/5 = 0.6
10/2 = 5
3/5 = 0.6
(The Question Begins Now) :D
If the decimals are repeating recursively, then enclose them inside ().
If the decimals are repeating recursively, then enclose them inside ().
eg.
8/3 = 2.(6)
8/3 = 2.(6)
as 8/3 = 2.66666666....... infinitly.
Input:
The first line of each test case is an integer N denoting the numerator of fraction.
The second line of each test case is an integer D denoting the denominator of fraction.
The second line of each test case is an integer D denoting the denominator of fraction.
Output:
Print decimal of that fraction in separate line for each test case.
Constraints:
Constraints:
1 ≤ N,D ≤ 2000
Example:
Example:
Input
4
2
4
2
8
3
Output
2
2.(6)
//JAVA CODE
import java.util.Scanner; public class Main { static String simple_fraction(int a,int b){ String s=a/b+""; if(a%b==0) return s; a%=b; String ad=""; int[]n=new int[b];int count=0; while(a>0 && n[a]==0){ n[a]=++count; a*=10; int temp=a/b; ad+=temp; a%=b; } count--; if(a>0){ int temp=n[a]-1; ad=ad.substring(0,temp)+"("+ad.substring(temp)+")"; } return s+"."+ad; } public static void main(String[] args) { Scanner sc= new Scanner(System.in); int a=sc.nextInt(); int b=sc.nextInt(); System.out.println(simple_fraction(a,b)); } }
Comments
Post a Comment