62,620
社区成员
发帖
与我相关
我的任务
分享
import java.util.Arrays;
import java.util.Comparator;
public class MergeSort {
public static <T> void merge(T[] a, int p, int q, int r,
Comparator<? super T> c) {
T[] left = Arrays.copyOfRange(a, p, q);
T[] right = Arrays.copyOfRange(a, q, r);
int indexLeft = 0;
int indexRight = 0;
for (int i = p; i < r; i++) {
if (indexLeft >= left.length) {
break;
}
if (indexRight >= right.length) {
System
.arraycopy(left, indexLeft, a, i, left.length
- indexLeft);
break;
}
if (c.compare(left[indexLeft], right[indexRight]) < 0) {
a[i] = left[indexLeft++];
} else {
a[i] = right[indexRight++];
}
}
}
public static <T> void mergeSort(T[] a, int p, int r,
Comparator<? super T> c) {
if (p + 1 < r) {
int q = (p + r) / 2;
mergeSort(a, p, q, c);
mergeSort(a, q + 1, r, c);
merge(a, p, q, r, c);
}
}
public static <T> void mergeSort(T[] a, Comparator<? super T> c) {
mergeSort(a, 0, a.length, c);
}
public static void main(String[] args) {
// merge's test
System.out.println("merge method test");
Integer[] tempMerge = new Integer[] { 1, 4, 7, 11, 14, 17, 2, 4, 6, 8,
10, 20, 30, 40 };
merge(tempMerge, 0, 6, tempMerge.length, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1 - o2;
}
});
for (int i : tempMerge) {
System.out.print(i + " ");
}
System.out.println();
// mergeSort's test
System.out.println("mergeSort method test");
Integer[] tempMergeSoft = new Integer[] { 5, 2, 4, 6, 1, 3, 2, 6 };
mergeSort(tempMergeSoft, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1 - o2;
}
});
for (int i : tempMergeSoft) {
System.out.print(i + " ");
}
System.out.println();
}
}


public class Test {
public static void main(String[] args) {
double n1 = pow(2, 6);
double n2 = pow(2, -6);
System.out.println(n1);
System.out.println(n2);
}
public static double pow(double base, int exponent) {
if (exponent < 0)
return 1 / pow(base, -exponent);
double power = 1;
while(exponent > 0) {
if((exponent & 1) == 1) {
power *= base;
}
base *= base;
exponent >>= 1;
}
return power;
}
}
1. static public Point multiply(Point p, int x, Line l) throws EllipticException {
2. mapResult.clear();
3. // mapResult.put(1, p);
4. return multiply(p, x, l, mapResult);
5. }
6. static private Point multiply(Point p, int x, Line l, Map<Integer, Point> mapResult)
7. throws EllipticException {
8. if (x == 1)
9. return p;
10. if (mapResult.containsKey(x))
11. return mapResult.get(x);
12. int half = x / 2;
13. while (half > 0) {
14. try {
15. Point point = plus(multiply(p, half, l, mapResult), multiply(p, x - half, l,
16. mapResult), l);
17. mapResult.put(x, point);
18. return point;
19. } catch (EllipticException ee) {
20. half--;
21. }
22. }
23. throw new EllipticException("can't multiply " + p + "*" + x + " mod " + l.p);
24. }