CodeForces 598C Nearest vectors
这题对精度要求很高。用atan2吧。。。
#include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> using namespace std; const int maxn=100000+10; int n; struct X { long double x,y; long double k; int id; }s[maxn]; bool cmp(const X&a,const X&b) { return a.k<b.k; } int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { cin>>s[i].x>>s[i].y; s[i].k=atan2(s[i].y,s[i].x); s[i].id=i; } sort(s+1,s+1+n,cmp); s[0]=s[n]; long double Min=99999; int ans1,ans2; for(int i=0;i<n;i++) { long double jiao=s[i+1].k-s[i].k; if(jiao<0) jiao+=acos(-1.0)*2; if(jiao<Min) { Min=jiao; ans1=s[i].id; ans2=s[i+1].id; } } printf("%d %d\n",ans1,ans2); return 0; }