題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2080
夾角有多大II
Problem Description
這次xhd面臨的問題是這樣的:在一個平面內(nèi)有兩個點,求兩個點分別和原點的連線的夾角的大小。
注:夾角的范圍[0,180],兩個點不會在圓心出現(xiàn)。
Input
輸入數(shù)據(jù)的第一行是一個數(shù)據(jù)T,表示有T組數(shù)據(jù)。
每組數(shù)據(jù)有四個實數(shù)x1,y1,x2,y2分別表示兩個點的坐標,這些實數(shù)的范圍是[-10000,10000]。
Output
對于每組輸入數(shù)據(jù),輸出夾角的大小精確到小數(shù)點后兩位。
Sample Input
2
1 1 2 2
1 1 1 0
Sample Output
0.00
45.00
解題思路:
a·b=(x1*x2+y1*y2)=|a||b|·cosθ
幾何意義,向量a在向量b方向上的投影與向量b的模的乘積
可以求出cosθ,利用反三角函數(shù)求出角度,由于是弧度制,要轉(zhuǎn)角度制,同時pi的精度要夠大。
解題中用到的一點技巧
1? pi=acos(-1);
2?sqrt( )函數(shù)開平方;
3 %.2f 控制輸出的格式
4?1度=π/180≈0.01745弧度,1弧度=180/π≈57.3度。
代碼如下:
#include<stdio.h>
#include<math.h>
int main()
{
int t;
double x1,y1,x2,y2;
double pi,sum,angle;
pi=acos(-1);
while(~scanf("%d",&t))
{
while(t--)
{
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
sum=(x1*x2+y1*y2)/(sqrt((x1*x1+y1*y1))*sqrt((x2*x2+y2*y2)));
angle=acos(sum)*180/pi;
printf("%.2f\n",angle);
}
}
return 0;
}