#include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; const int maxn=10000+5; const double eps=1e-10; int dcmp(double x) { if(fabs(x)<eps) return 0; return x<0?-1:1; }struct Point { double x,y; int id; Point(){} Point(double x,double y,int id):x(x),y(y),id(id){} }P[maxn]; typedef Point Vector; Vector operator-(Point A,Point B) { return Vector(A.x-B.x,A.y-B.y,0); }double Cross(Vector A,Vector B) { return A.x*B.y-A.y*B.x; }bool cmp(Point A,Point B) { return dcmp(Cross(A-P[0],B-P[0]))<0; }int main() { int n; while(scanf("%d",&n)==1) { scanf("%lf%lf",&P[0].x,&P[0].y); P[0].id=1; for(int i=1;i<n;++i) { scanf("%lf%lf",&P[i].x,&P[i].y); P[i].id=i+1; if(P[i].x<P[0].x || (P[i].x==P[0].x && P[i].y<P[0].y) ) swap(P[0],P[i]); }sort(P+1,P+n,cmp); printf("%d %d\n",P[0].id,P[n/2].id); }return 0; }转为Java代码
时间: 2024-04-28 17:24:40 浏览: 89
import java.util.*;
import java.io.*;
class Main {
static final double eps = 1e-10;
static int n;
static Point[] P = new Point[10000 + 5];
static class Point {
double x, y;
int id;
public Point() {}
public Point(double x, double y, int id) {
this.x = x;
this.y = y;
this.id = id;
}
}
static int dcmp(double x) {
if (Math.abs(x) < eps) return 0;
return x < 0 ? -1 : 1;
}
static class Vector {
double x, y;
public Vector(double x, double y) {
this.x = x;
this.y = y;
}
}
static Vector minus(Point A, Point B) {
return new Vector(A.x - B.x, A.y - B.y);
}
static double Cross(Vector A, Vector B) {
return A.x * B.y - A.y * B.x;
}
static boolean cmp(Point A, Point B) {
return dcmp(Cross(minus(A, P[0]), minus(B, P[0]))) < 0;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
n = sc.nextInt();
P[0] = new Point(sc.nextDouble(), sc.nextDouble(), 1);
for (int i = 1; i < n; ++i) {
P[i] = new Point(sc.nextDouble(), sc.nextDouble(), i + 1);
if (P[i].x < P[0].x || (P[i].x == P[0].x && P[i].y < P[0].y))
swap(P, 0, i);
}
Arrays.sort(P, 1, n, Main::cmp);
System.out.println(P[0].id + " " + P[n / 2].id);
}
}
static void swap(Point[] P, int i, int j) {
Point temp = P[i];
P[i] = P[j];
P[j] = temp;
}
}
阅读全文