湖南大学ACM程序设计新生杯大赛(C-Do you like banana?)

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述 

湖南大学ACM程序设计新生杯大赛(C-Do you like banana?)_第1张图片
Two endpoints of two line segments on a plane are given to determine whether the two segments are intersected (there is a common point or there is a partial coincidence that intersects). If intersected, output "Yes", otherwise output "No".

输入描述:

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
代码:
 
       
#include
#include
int T;
double x1, y, x2, y2, x3, y3, x4, y4;
void fun()
{
	double k1, k2, b1, b2;
	k1 = (y2 - y) / (x2 - x1);
	k2 = (y4 - y3) / (x4 - x3);
	b1 = y - k1*x1;
	b2 = y3 - k2*x3;
	double t;
	if (x1>x2) { t = x1; x1 = x2; x2 = t; }
	if (x3>x4) { t = x3; x3 = x4; x4 = t; }
	if (y>y2) { t = y; y = y2; y2 = t; }
	if (y3>y4) { t = y3; y3 = y4; y4 = t; }
	if (k1 == k2 && b1 != b2) { printf("No\n"); return; }
	else if (k1 == k2 && b1==b2)
	{
		if (x4x2) { printf("No\n"); return; }
		else { printf("Yes\n"); return; }
	}
	else if (k1 != k2)
	{
		double x = (b2 - b1) / (k1 - k2);
		if (x <= x2&&x >= x1&&x <= x4&&x >= x3) { printf("Yes\n"); return; }
		else { printf("No\n"); return; }
	}
}
int main()
{
	scanf("%d", &T);
	while (T--)
	{
		scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &x1, &y, &x2, &y2, &x3, &y3, &x4, &y4);
		fun();
	}
	return 0;
}

有问题欢迎私聊和评论~么么哒~(难的不会写 哈哈 只有简单的)

你可能感兴趣的:(牛客竞赛)