Points on Cycle (hdu1700,幾何)

尋找&星空の孩子發表於2013-08-16

Points on Cycle

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1303    Accepted Submission(s): 459

Problem Description

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
 

 

題意:(摘自網路)一個以原點為中心的圓,告訴你圓上的一個點,求與另外的兩個點組成的三角形的周長最長的兩點作標。
根據幾何知識,知道圓內等邊三角形的周長最長。所以題目轉化為求已知一個點的圓內接等邊三角形的另兩點作標。  
思路:設P(x,y),一個方程是x*x+y*y=r*r;另一個方程是根據向量知識,向量的夾角公式得到方程。
因為圓心角夾角為120度,已知一個向量(即一個點作標),所以夾角公式:COS(2PI/3)=a*b/|a|*|b|;(a,b為向量);
已知角和a向量,就可求b向量b(x,y).由方程組可求得(x,y);最後得到的是一元二次方程組,可得到兩個解,即為兩個點的作標。

 

 

  下面是我自己的所作的詳解:

                
 

ps:http://acm.hdu.edu.cn/showproblem.php?pid=1700

 

#include<stdio.h>
#include<math.h>
int main()
{
    double x1,x2,x3,y1,y2,y3,a,b,c,r;
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lf%lf",&x1,&y1);
        r=sqrt(x1*x1+y1*y1);
        a=1;
        b=y1;
        c=r*r/4-x1*x1;
        y2=(-b-sqrt(b*b-4*a*c))/(2*a);
        y3=(-b+sqrt(b*b-4*a*c))/(2*a);
        if(fabs(x1-0)<1e-7)//注意這裡不能是x1==0
        {
            x2=-sqrt(r*r-y2*y2);
            x3=sqrt(r*r-y3*y3);
        }
        else
        {
            x2=(-r*r/2-y1*y2)/x1;
            x3=(-r*r/2-y1*y3)/x1;
        }
        printf("%.3lf %.3lf %.3lf %.3lf\n",x2,y2,x3,y3);
        
    }
    return 0;
}

 

我自己錯了好幾次的,,,,囧rz    寫的這麼認真,這麼詳細了,真的很辛苦,
    
  
  
  
  
  
  
  
  
  
  
  
 
 
 


 

程式碼如下:

 

相關文章