题目链接在这里
题目描述
There is a cycle with its center on the origin.
Now give you a point on the cycle, you are to find out the other two points on it, to maximize the sum of the distance between each other
you may assume that the radius of the cycle will not exceed 1000.
Input
There are T test cases, in each case there are 2 decimal number representing the coordinate of the given point.
Output
For each testcase you are supposed to output the coordinates of both of the unknow points by 3 decimal places of precision
Alway output the lower one first(with a smaller Y-coordinate value), if they have the same Y value output the one with a smaller X.
Note
when output, if the absolute difference between the coordinate values X1 and X2 is smaller than 0.0005, we assume they are equal.
Sample Input
2
1.500 2.000
563.585 1.251
Sample Output
0.982 -2.299 -2.482 0.299
-280.709 -488.704 -282.876 487.453
大概意思就是有一圆心在原点的圆,给出一个圆上的点,求出另外两个点使该三点组成的三角形周长最大。当三角形为等边三角形的时候周长是最大的。
结题思路是根据圆的性质和向量来构造方程组,等边三角形每两个点和圆心构成的夹角都是2π/3
x ^ 2 + y ^ 2 = r ^ 2
ax + by = r ^ 2 * cos(2π/3)
解方程组就行了
代码
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
int main(){
int T;
double a, b;
double sq3 = sqrt(3);
scanf("%d", &T);
while(T--){
scanf("%lf%lf", &a, &b);
double x1, x2, y1, y2;
double r = sqrt(a * a + b * b);
x2 = (-a - sq3 * b * 1.0) / 2;
x1 = (-a + sq3 * b * 1.0) / 2;
y1 = (-b - sq3 * a * 1.0) / 2;
y2 = (-b + sq3 * a * 1.0) / 2;
if(y1 > y2 || fabs(y1 - y2) < 1e-5 && x1 > x2){
swap(x1, x2);
swap(y1, y2);
}
printf("%.3lf %.3lf %.3lf %.3lf\n", x1, y1, x2, y2);
}
return 0;
}