Posted on 2010-08-07 15:11
MiYu 閱讀(628)
評論(2) 編輯 收藏 引用 所屬分類:
ACM ( 數論 )
MiYu原創, 轉帖請注明 : 轉載自 ______________白白の屋
題目地址:
http://acm.hdu.edu.cn/showproblem.php?pid=2080
題目描述:
Problem Description
這次xhd面臨的問題是這樣的:在一個平面內有兩個點,求兩個點分別和原點的連線的夾角的大小。
注:夾角的范圍[0,180],兩個點不會在圓心出現。
Input
輸入數據的第一行是一個數據T,表示有T組數據。
每組數據有四個實數x1,y1,x2,y2分別表示兩個點的坐標,這些實數的范圍是[-10000,10000]。
Output
對于每組輸入數據,輸出夾角的大小精確到小數點后兩位。
Sample Input
2
1 1 2 2
1 1 1 0
Sample Output
0.00
45.00
題目分析:
純數學題. 有多邊型面積公式, 我們可以得到 三角型的面積 :
S = ( x0 * y1 - x1 * y0 ) / 2.
而由三角型正弦定理我們知道 :
S = 1 / 2 * A * B * sinV . 聯立 2方程就可以得到 夾角V 的解
的方程 :
sinV = ( x0 * y1 - x1 * y0 ) / A / B ; 最后利用 c 數學庫函數 asin就可以求出V的弧
度值, 把弧度轉換成角度就可以了. (
注意平角和鈍角的判斷 )
代碼如下 :
MiYu原創, 轉帖請注明 : 轉載自 ______________白白の屋
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main ()
{
int T;
cin >> T;
while ( T -- )
{
double x0,x1,y0,y1;
cin >> x0 >> y0 >> x1 >> y1;
double A = sqrt ( x0 * x0 * 1.0 + y0 * y0 * 1.0 );
double B = sqrt ( x1 * x1 * 1.0 + y1 * y1 * 1.0 );
double resin = ( fabs ( x0 * y1 - x1 * y0 ) ) / A / B ;
double res = asin ( resin ) * 180.0 / acos( -1 );
res = res < 1e-5 ? ( x0 * x1 < 0 ? 180.0 : 0.0 ) //平角
: ( x0 * x1 <= 0 && y0 * y1 <= 0 ? 180.0 - res : res ); //是否鈍角
cout << setprecision (2) << setiosflags ( ios::fixed ) << res << endl;
}
return 0;
}