-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEgyptFractToFract.java
More file actions
51 lines (38 loc) · 1.17 KB
/
EgyptFractToFract.java
File metadata and controls
51 lines (38 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//2007 - #2
import java.util.*;
public class EgyptFractToFract {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
Scanner inputString = new Scanner(System.in);
int times = input.nextInt();
for(int i = 0; i < times; i++){
int numFractions = input.nextInt();
String rawDenoms = inputString.nextLine();
String[] raws = rawDenoms.split(" ");
int[] denoms = new int[raws.length];
int newDenom = 1;
int numer = 0;
for(int x = 0; x < raws.length; x++){
denoms[x] = Integer.parseInt(raws[x]);
newDenom *= denoms[x];
}
for(int x = 0; x < denoms.length; x++){
numer += newDenom / denoms[x];;
}
//Reduce numer/newDenom
for(int x = numer; x > 0; x--){
if(newDenom % x == 0){
newDenom /= x;
numer = x;
}
}
for(int x = 0; x < denoms.length; x++){
if(x == denoms.length - 1){
System.out.print("1/" + denoms[x] + " = ");
}
System.out.print("1/" + denoms[x] + " + ");
}
System.out.println(numer + "/" + newDenom);
}
}
}