[HDU 4082] Hou Yi's secret (简单计算几何)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4082

题目大意:

给你n个点,问能最多构成多少个相似三角形。

用余弦定理,计算三个角度,然后暴力数有多少个,更新答案。

代码:

 #include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <set>
using namespace std;
typedef pair<int,int> PII;
typedef long long LL; #define EPS 1e-8
#define PB push_back struct POINT{
int x,y;
double dis(const POINT& b){
return sqrt( double(x-b.x)*double(x-b.x)+double(y-b.y)*double(y-b.y) );
}
POINT(int ax=,int ay=):x(ax),y(ay){}
bool operator==(const POINT &b) const{
return x==b.x&&y==b.y;
}
bool operator<(const POINT &b) const{
if( x==b.x ) return y<b.y;
return x<b.x;
}
}; struct triangle{
double cosa[];
triangle(){} triangle(POINT a,POINT b,POINT c){
double A = a.dis(b);
double B = a.dis(c);
double C = b.dis(c); cosa[] = (A*A+B*B-C*C)/(*A*B);
cosa[] = (B*B+C*C-A*A)/(*B*C);
cosa[] = (A*A+C*C-B*B)/(*A*C);
sort(cosa,cosa+);
} triangle(double a,double b,double c){
cosa[] = a;
cosa[] = b;
cosa[] = c;
sort(cosa,cosa+);
} bool operator==(const triangle& b) const{
return fabs(cosa[]-b.cosa[])<EPS&&fabs(cosa[]-b.cosa[])<EPS&&fabs(cosa[]-b.cosa[])<EPS;
} }; bool isLine(POINT a,POINT b,POINT c){
return (c.x-a.x)*(b.y-a.y) == (b.x-a.x)*(c.y-a.y);
} POINT pt[];
int n; int main(){
while( scanf("%d",&n)!=EOF && n ){
set<POINT> st;
int ptrb = ;
for(int i=;i<n;i++){
int x,y;
scanf("%d%d",&x,&y);
if( st.find(POINT(x,y))==st.end() ){
pt[ptrb++] = POINT(x,y);
st.insert(POINT(x,y));
}
} vector<triangle> vec;
for(int i=;i<ptrb;i++){
for(int j=i+;j<ptrb;j++){
for(int k=j+;k<ptrb;k++){
if( !isLine(pt[i],pt[j],pt[k])){
vec.PB(triangle(pt[i],pt[j],pt[k]));
}
}
}
} int maxn = ; for(size_t i=;i<vec.size();i++){
int as = ; for(size_t j=i+;j<vec.size();j++){
if( vec[i]==vec[j] ) as++;
}
maxn = max(as,maxn);
}
printf("%d\n",maxn);
}
return ;
}
上一篇:侧菜单栏的实现SlidingPaneLayout


下一篇:Galera集群server.cnf参数调整--前言