2831. Vectors

At a geometry lesson Gerald was given a task: to get vector B out of vector A. Besides, the teacher permitted him to perform the following operations with vector A:

Turn the vector by 90 degrees clockwise.
Add to the vector a certain vector C.
Operations could be performed in any order any number of times.

Can Gerald cope with the task?

Input
The first line contains integers x1 , y1 − the coordinates of the vector A (-108≤x1,y1≤108). The second and the third line contain in the similar manner vectors B and C (their coordinates are integers; their absolute value does not exceed 108).

Output
Print “YES” (without the quotes) if it is possible to get vector B using the given operations. Otherwise print “NO” (without the quotes).

Examples
Input

0 0
1 1
0 1

Output

YES

Input

0 0
1 1
1 1

Output

YES

Input

0 0
1 1
2 2

Output

NO

这道题的意思是对向量 A 可以进行 “顺时针旋转90°” 以及 “加上向量C” 这两种操作,操作次数与顺序不限,最后看能不能得出向量 B。
我的解法如下:

#include <stdio.h>

typedef struct
{
	int x;
	int y;
}myVector;

//向量加法
myVector vectorAdd(myVector A, myVector C)
{
	myVector temp;
	temp.x = A.x + C.x;
	temp.y = A.y + C.y;
	return temp;
}

//向量旋转90°
myVector vectorRotate(myVector A)
{
	//旋转规则:(x, y) -> (y, -x)
	myVector temp;
	temp.x = A.y;
	temp.y = -A.x;
	return temp;
}

//比较两个向量的大小关系
//如果 A 的横坐标或纵坐标比 B 大,则定义 A > B
int vectorCompare(myVector A, myVector B)
{
	if (A.x == B.x && A.y == B.y) return 1;		//相等返回 1
	else if (A.x > B.x || A.y > B.y) return 2;			//若 A > B,返回 2
	else return 0;												//否则返回 0
}

int main()
{
	myVector A, B, C;
	scanf("%d %d", &A.x, &A.y);
	scanf("%d %d", &B.x, &B.y);
	scanf("%d %d", &C.x, &C.y);

	myVector temp;
	int i = 0;

	if (A.x + A.y + C.x + C.y == 0 && B.x + B.y != 0) {		//当 A 和 C 为零向量,但 B 不为零向量时
		printf("NO");
		return 0;
	}

	while (vectorCompare(A, B) == 0)
	{
		//旋转一次
		temp = vectorRotate(A);
		if (vectorCompare(A, temp) == 1) {		//旋转后为本身,则令 A 加上 C 
			A = vectorAdd(A, C);
			continue;
		}
		
		//相加一次
		temp = vectorAdd(temp, C);
		i++;													// i 用来记录旋转次数
		if (vectorCompare(temp, B) == 0) {
			if (i % 4 == 0) A = vectorAdd(A, C);		//旋转四次需要将 A + C
		}
		else {
			A = temp;
			break;
		}
	}

	if (vectorCompare(A, B) == 1)
		printf("YES");
	else if (vectorCompare(A, B) == 2)
		printf("NO");
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值