標籤:-- iostream map main 關注 algo cpp cout 時間
1264 線段相交基準時間限制:1 秒 空間限制:131072 KB 分值: 0 難度:基礎題 收藏 關注描述
給出平面上兩條線段的兩個端點,判斷這兩條線段是否相交(有一個公用點或有部分重合認為相交)。 如果相交,輸出"Yes",否則輸出"No"。
Input
第1行:一個數T,表示輸入的測試數量(1 <= T <= 1000)
第2 - T + 1行:每行8個數,x1,y1,x2,y2,x3,y3,x4,y4。(-10^8 <= xi, yi <= 10^8)
(直線1的兩個端點為x1,y1 | x2, y2,直線2的兩個端點為x3,y3 | x4, y4)
Output
輸出共T行,如果相交輸出"Yes",否則輸出"No"。
Input樣本
2
1 2 2 1 0 0 2 2
-1 1 1 1 0 0 1 -1
Output樣本
Yes
No
題解
通過判斷叉積來判斷線段是否相交。代碼如下:
#include <cstdio>#include <iostream>#include <algorithm>#include <string>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <map>#include <set>#include <queue>#include <utility>#define ll long longusing namespace std ;struct point {double x , y ;} ;double xmul ( point a , point b , point c ){ return ( b.x - a.x ) * ( c.y - a.y ) - ( b.y - a.y ) * ( c.x - a.x ) ;}int main(){ int t ; cin >> t ; while ( t -- ){ point a , b , c , d ; cin >> a.x >> a.y >> b.x >> b.y >> c.x >> c.y >> d.x >> d.y ; if ( xmul(a , b , c) * xmul(a , b , d) <= 0 && xmul(c , d , a) * xmul(c , d , b) <= 0 ){ cout << "Yes" << endl ; }else{ cout << "No" << endl ; } } return 0 ;}
51Nod 1264 線段相交