-
Notifications
You must be signed in to change notification settings - Fork 0
/
HW08_4111056036_5.java
59 lines (49 loc) · 1.4 KB
/
HW08_4111056036_5.java
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
public class HW08_4111056036_5 extends LLK {
public int gcd(int a, int b) {
int temp;
while (b != 0) {
temp = a;
a = b;
b = temp % b;
}
return Math.abs(a);
}
@Override
public boolean checkLLK(int[][] array) {
int n = array.length;
if(n < 3) {
return true;
}
int firstDeltaX = array[0][0] - array[1][0];
int firstDeltaY = array[0][1] - array[1][1];
int gcd = gcd(firstDeltaX, firstDeltaY);
firstDeltaX /= gcd;
firstDeltaY /= gcd;
if(firstDeltaX < 0) {
firstDeltaX = ~firstDeltaX + 1;
firstDeltaY = ~firstDeltaY + 1;
}
int deltaX, deltaY;
for(int i = 2; i < n; ++i) {
deltaX = array[i][0] - array[i - 1][0];
deltaY = array[i][1] - array[i - 1][1];
if(firstDeltaX == 0 && deltaX == 0) {
continue;
}
if(firstDeltaY == 0 && deltaY == 0) {
continue;
}
gcd = gcd(deltaX, deltaY);
deltaX /= gcd;
deltaY /= gcd;
if(deltaX < 0) {
deltaX = ~deltaX + 1;
deltaY = ~deltaY + 1;
}
if(deltaX != firstDeltaX || deltaY != firstDeltaY) {
return false;
}
}
return true;
}
}