HDOJ题目地址:传送门
2 1 1 2 2 1 1 1 0
0.00 45.00
第一种方法:使用余弦定理来求解,对于边长为a、b、c而相应角为A、B、C的三角形,
有:a² = b² + c²- 2bc·cosA
#include<iostream> #include<stdio.h> #include<math.h> using namespace std; int main(){ int n; double PI=3.1415926; double x1,x2,y1,y2,a,b,c,result; cin>>n; while(n--){ cin>>x1>>y1>>x2>>y2; a=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); b=sqrt(x1*x1+y1*y1); c=sqrt(x2*x2+y2*y2); double g=(b*b+c*c-a*a)/(2*b*c); result=acos(g); printf("%.2lf\n",result*180.00/PI); } }
第二种方法:使用向量积来求解,
#include<iostream> #include<stdio.h> #include<math.h> using namespace std; int main(){ int n; double PI=3.1415926; double x1,x2,y1,y2,a,b,c,result; cin>>n; while(n--){ cin>>x1>>y1>>x2>>y2; a=x1*x2+y1*y2; b=sqrt(x1*x1+y1*y1); c=sqrt(x2*x2+y2*y2); double g=a/(b*c); result=acos(g); printf("%.2lf\n",result*180.00/PI); } }