public static int compare(int x, int y) { return (x < y) ? -1 : ((x == y) ? 0 : 1); } 比较器返回-1 0 1 分别代表正序还是逆序呀
时间: 2024-06-08 08:12:13 浏览: 84
这个比较器返回-1、0、1分别代表小于、等于、大于。通常在排序算法中,当返回结果为负数时表示要将第一个参数排在第二个参数前面,返回结果为正数时表示要将第二个参数排在第一个参数前面,返回结果为零时表示两个参数相等,不需要改变它们的顺序。因此,在这个比较器中,如果x小于y,则返回-1,如果x等于y,则返回0,如果x大于y,则返回1。
相关问题
解释一下这段代码并说出它的计算复杂度import java.util.Arrays; import java.util.Scanner; public class p1160 { static class Node implements Comparable<Node> { int x, y, len; public Node(int x, int y, int len) { this.x = x; this.y = y; this.len = len; } @Override public int compareTo(Node other) { return Integer.compare(this.len, other.len); } } static int[] fa; static int find(int x) { if (fa[x] != x) fa[x] = find(fa[x]); return fa[x]; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int m = scanner.nextInt(); fa = new int[n + 1]; for (int i = 1; i <= n; i++) fa[i] = i; Node[] p = new Node[m + 1]; for (int i = 1; i <= m; i++) { int x = scanner.nextInt(); int y = scanner.nextInt(); int len = scanner.nextInt(); p[i] = new Node(x, y, len); } Arrays.sort(p, 1, m + 1); int s = 0, maxz = 0; int i; for (i = 1; i <= m && s < n - 1; i++) { int tx = find(p[i].x); int ty = find(p[i].y); if (tx != ty) { fa[tx] = ty; s++; maxz = Math.max(maxz, p[i].len); } } System.out.println(maxz); System.out.println(i - 1); for (int j = 1; j < i; j++) System.out.println(p[j].x + " " + p[j].y); } }
这段代码实现了一个最小生成树算法,采用的是Kruskal算法。输入的第一行包含两个整数n和m,分别表示点的个数和边的个数。接下来m行每行包含三个整数x、y、len,表示点x和点y之间有一条长度为len的边。程序会输出最小生成树的最大边权值,以及最小生成树的所有边。
该算法的时间复杂度为O(mlogm),其中m是边的数量。主要由于排序的时间复杂度为O(mlogm)。
#include<iostream> #include<cmath> #include<algorithm> using namespace std; struct point { int x, y, index; double theta; } p[30000], temp; int f(point& a, point& b) { if (a.theta == b.theta) { if (a.x == b.x && a.y > 0) return a.y * a.y > b.y * b.y; else return a.x * a.x + a.y * a.y < b.x* b.x + b.y * b.y; }return a.theta < b.theta; }int main() { int n; cin >> n; //input for (int i = 0; i < n; i++) { int x, y; cin >> p[i].x >> p[i].y; p[i].index = i + 1; }//exchange min_x to first position int min = p[0].x, minPosition = 0; for (int i = 1; i < n; i++) { minPosition = min <= p[i].x ? minPosition : i; min = min <= p[i].x ? min : p[i].x; }temp = p[0]; p[0] = p[minPosition]; p[minPosition] = temp; //cal atan2 for (int i = 1; i < n; i++) { p[i].x -= p[0].x; p[i].y -= p[0].y; p[i].theta = atan2(p[i].y, p[i].x); }//sort exclude min_x sort(p + 1, p + n, f); //output cout << n << endl; for (int i = 0; i < n; i++) { if (p[i].index == 1) { for (int j = i; j < n; j++) cout << p[j].index << endl; for (int j = 0; j < i; j++) cout << p[j].index << endl; break; } } }转为Java
以下是将给定的 C++ 代码转换为 Java 代码的结果:
```java
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
static class Point {
int x, y, index;
double theta;
public Point(int x, int y, int index, double theta) {
this.x = x;
this.y = y;
this.index = index;
this.theta = theta;
}
}
static Point[] p = new Point[30000];
static int f(Point a, Point b) {
if (a.theta == b.theta) {
if (a.x == b.x && a.y > 0)
return a.y * a.y > b.y * b.y ? 1 : -1;
else
return a.x * a.x + a.y * a.y < b.x * b.x + b.y * b.y ? 1 : -1;
}
return a.theta < b.theta ? 1 : -1;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
// input
for (int i = 0; i < n; i++) {
int x = scan.nextInt();
int y = scan.nextInt();
p[i] = new Point(x, y, i + 1, 0);
}
// exchange min_x to first position
int min = p[0].x, minPosition = 0;
for (int i = 1; i < n; i++) {
minPosition = min <= p[i].x ? minPosition : i;
min = min <= p[i].x ? min : p[i].x;
}
Point temp = p[0];
p[0] = p[minPosition];
p[minPosition] = temp;
// cal atan2
for (int i = 1; i < n; i++) {
p[i].x -= p[0].x;
p[i].y -= p[0].y;
p[i].theta = Math.atan2(p[i].y, p[i].x);
}
// sort exclude min_x
Arrays.sort(p, 1, n, new Comparator<Point>() {
@Override
public int compare(Point a, Point b) {
return f(a, b);
}
});
// output
System.out.println(n);
for (int i = 0; i < n; i++) {
if (p[i].index == 1) {
for (int j = i; j < n; j++)
System.out.println(p[j].index);
for (int j = 0; j < i; j++)
System.out.println(p[j].index);
break;
}
}
scan.close();
}
}
```
阅读全文