-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCubePerfect.java
More file actions
59 lines (52 loc) · 1.5 KB
/
Copy pathCubePerfect.java
File metadata and controls
59 lines (52 loc) · 1.5 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
52
53
54
55
56
57
58
59
public class CubePerfect {
public static void main(String[] args) {
System.out.println(isCubePerfect(new int[]{1, 1, 1, 1}));
System.out.println(isCubePerfect(new int[]{64}));
System.out.println(isCubePerfect(new int[]{63}));
System.out.println(isCubePerfect(new int[]{-1, 0, 1}));
System.out.println(isCubePerfect(new int[]{}));
System.out.println(isCubePerfect(new int[]{3, 7, 21, 36}));
}
static int isCubePerfect(int[] a) {
if(a.length == 0)
return 1;
for (int i = 0; i < a.length; i++) {
if (a[i] == 0 || a[i] == 1 || a[i] == -1)
continue;
boolean isCube = false;
for (int j = 2; j < Math.abs(a[i]) / 2; j++) {
if (a[i] > 1) {
if (a[i] == Math.pow(j, 3)) {
isCube = true;
break;
}
}
if (a[i] < -1) {
if (a[i] == Math.pow(-j, 3)) {
isCube = true;
break;
}
}
}
if(!isCube)
return 0;
}
return 1;
}
static int isCubePerfect1(int[] a) {
if (a.length == 0)
return 1;
for (int anA : a) {
anA = anA > 0 ? anA : -anA;
int number = 1;
int cube = 0;
while (cube < anA) {
cube = number * number * number;
number++;
}
if (cube != anA)
return 0;
}
return 1;
}
}