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;
}