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

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

题目描述 


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<stdio.h>
#include<math.h>
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 (x4<x1 || x3>x2) { 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;
}

有问题欢迎私聊和评论~么么哒~(难的不会写 哈哈 只有简单的)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值