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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
import static org.junit.Assert.*;
import org.junit.Test;
public class FractsTest {
private static String Array2D2String(long[][] lst) {
String s = "["; int l = lst.length;
for (int i = 0; i < l; i++) {
long[] a = lst[i];
s += "[" + a[0] + ", " + a[1];
if (i < l-1) s += "], ";
else s += "]";
}
return s += "]";
}
private static void testing(long[][] lst, String expected) {
System.out.println("Testing " + Array2D2String(lst));
String actual = Fracts.convertFrac(lst);
System.out.println("Actual " + actual);
System.out.println("Expect " + expected);
System.out.println("-");
assertEquals(expected, actual);
}
@Test
public void test() {
long[][] lst = new long[][] { {1, 2}, {1, 3}, {1, 4} };
testing(lst, "(6,12)(4,12)(3,12)");
lst = new long[][] { {69, 130}, {87, 1310}, {30, 40} };
testing(lst, "(18078,34060)(2262,34060)(25545,34060)");
lst = new long[][] { };
testing(lst, "");
lst = new long[][] { {77, 130}, {84, 131}, {3, 4} };
testing(lst, "(20174,34060)(21840,34060)(25545,34060)");
lst = new long[][] { {6, 13}, {187, 1310}, {31, 41} };
testing(lst, "(322260,698230)(99671,698230)(527930,698230)");
lst = new long[][] { {8, 15}, {7, 111}, {4, 25} };
testing(lst, "(1480,2775)(175,2775)(444,2775)");
lst = new long[][] { {1, 2}, {1, 3}, {1, 4} };
testing(lst, "(6,12)(4,12)(3,12)");
lst = new long[][] { {77, 130}, {840, 1310}, {3, 4} };
testing(lst, "(20174,34060)(21840,34060)(25545,34060)");
lst = new long[][] { {1, 100}, {30, 10000}, {1, 2500}, {1, 20000} };
testing(lst, "(200,20000)(60,20000)(8,20000)(1,20000)");
lst = new long[][] { {1, 1}, {3, 1}, {4, 1}, {5, 1} };
testing(lst, "(1,1)(3,1)(4,1)(5,1)");
lst = new long[][] { {3, 1} };
testing(lst, "(3,1)");
lst = new long[][] { {77, 130}, {84, 131}, {30, 40} };
testing(lst, "(20174,34060)(21840,34060)(25545,34060)");
lst = new long[][] { {1, 100}, {3, 1000}, {1, 2500}, {1, 20000} };
testing(lst, "(200,20000)(60,20000)(8,20000)(1,20000)");
}
private static int randInt(int min, int max) {
return (int)(min + Math.random() * ((max - min) + 1));
}
//
private static long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
private static long lcm(long a, long b) {
return a * b / gcd(a, b);
}
private static String convertFracHD(long[][] lst) {
long lcmall = 1;
long[][] newlst = new long[lst.length][2];
for (int i = 0; i < lst.length; i++) {
long g = gcd(lst[i][0], lst[i][1]);
newlst[i][0] = lst[i][0] / g;
newlst[i][1] = lst[i][1] / g;
}
//System.out.println(Arrays.deepToString(newlst));
for (long[] item : newlst) {
lcmall = lcm(lcmall, item[1]);
}
String result = "";
for (long[] item : newlst) {
result += "(" + (item[0] * lcmall / item[1]) + "," + lcmall + ")";
}
return result;
}
//
private static long[][] doEx(int n) {
long[][] out = new long[n][2];
int j = 0;
while (j < n) {
out[j][0] = randInt(10, 80);
out[j][1] = randInt(2, 5) * out[j][0];
j += 1;
}
return out;
}
@Test
public void test1() {
System.out.println("Random Tests ****");
for (int i = 0; i < 100; i++) {
long[][] v = doEx(randInt(5, 10));
String exp = convertFracHD(v);
testing(v, exp);
}
}
}
|