Problem Statement
????
双倍超立方数是指一个正整数可以正好被拆分为两种不同的a^3+b^3的方式,其中a,b均为整数且0<a<=b。对于任何一个指定的 int n, 返回所有的小于等于n的双倍超立方数的个数。
Definition
????
Class:
TwiceSuperCubic
Method:
count
Parameters:
int
Returns:
int
Method signature:
int count(int n)
(be sure your method is public)
????
Constraints
-
n取值范围为1到1,000,000,000(含)
Examples
0)
????
1
Returns: 0
1)
????
1729
Returns: 1
1729=1^3+12^3 1729=9^3+10^3
2)
????
475574
Returns: 27
import java.util.Arrays;
import java.util.Vector;
public class Second {
public static void main(String[] args) {
Second s = new Second();
long st = System.currentTimeMillis();
System.out.println(s.count(475574));
System.out.println(System.currentTimeMillis() - st);
}
public int count(int number) {
Vector<Integer> vec = new Vector<Integer>();
int max = (int) Math.cbrt(number);
for (int i = 1; i <= max; i++) {
for (int j = max; j > i; j--) {
int temp = addThree(i, j);
if (temp <= number) {
vec.add(new Integer(temp));
}
}
}
int size = vec.size();
int result[] = new int[size];
for (int i = 0; i < size; i++) {
result[i] = vec.elementAt(i).intValue();
}
Arrays.sort(result);
int count = 0;
for (int i = 0; i < result.length;) {
int n = countNumber(result, i);
if (n == 2) {
count++;
}
i += n;
}
return count;
}
public int addThree(int a, int b) {
return a * a * a + b * b * b;
}
public int countNumber(int[] array, int st) {
int number = 1;
int len = array.length;
for (int i = st + 1; i < len; i++) {
if (array[i] == array[st]) {
number++;
}
}
return number;
}
}