NC14625. Do you like Banana ?
描述
输入描述
The first line is a number of T, indicating the number of tests inputed (1 <= T <= 1000)
For next T line,each line contains 8 numbers , x1,y1,x2,y2,x3,y3,x4, y4. (8-10 ^ < = xi, yi < = 10 ^ 8)
(the two endpoints of line 1 are x1, y1, |, x2, y2, and two of the endpoints of line 2 are x3, y3, |, x4, y4).
输出描述
For each test case, output"Yes" if the two segments intersected, else output"No".
示例1
输入:
2 1 2 2 1 0 0 2 2 -1 1 1 1 0 0 1 -1
输出:
Yes No
C++14(g++5.4) 解法, 执行用时: 9ms, 内存消耗: 504K, 提交时间: 2020-08-05 11:07:51
#include <iostream> using namespace std; struct Node { double x; double y; }; double cross(Node q, Node b, Node c) { return (q.x-b.x)*(c.y-b.y)-(q.y-b.y)*(c.x-b.x); } int main() { int t; cin >> t; while(t--){ Node a, b, c, d; cin >> a.x >> a.y >> b.x >> b.y >> c.x >> c.y >> d.x >> d.y; if(cross(a,d,c)*cross(b,d,c)<=0&&cross(c,a,b)*cross(d,a,b)<=0){ cout << "Yes" << endl; } else{ cout << "No" << endl; } } }
C++11(clang++ 3.9) 解法, 执行用时: 8ms, 内存消耗: 504K, 提交时间: 2020-02-29 22:44:56
#include<iostream> using namespace std; int main() { int t; cin>>t; int x1,x2,x3,x4,y1,y2,y3,y4; while(t--) { cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4; double t=((y2-y1)*1.0/(x2-x1)*x1-(y4-y3)*1.0/(x4-x3)*x3+y3-y1)/((y2-y1)*1.0/(x2-x1)-(y4-y3)*1.0/(x4-x3)); if(t>=min(x1,x2)&&t<=max(x1,x2)&&t>=min(x3,x4)&&t<=max(x3,x4)) cout<<"Yes"<<endl; else cout<<"No"<<endl; } return 0; }