首页 > ACM题库 > HDU-杭电 > HDU 1700 Points on Cycle -数学相关-[解题报告] C++
2013
12-21

HDU 1700 Points on Cycle -数学相关-[解题报告] C++

Points on Cycle

问题描述 :

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.

输入:

There are T test cases, in each case there are 2 decimal number representing the coordinate of the given point.

输出:

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.

样例输入:

2
1.500 2.000
563.585 1.251

样例输出:

0.982 -2.299 -2.482 0.299
-280.709 -488.704 -282.876 487.453

题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1700

给你一个点在半径为r的圆上,该圆圆心在原点上,让你求圆上两个点是这两个点与给出这个点的距离最大

显然是互为120度的时候最大。

首先可以求出圆的半径,然后用两个公式求解方程组

a*b=|a|*|b|*cos(120);

x*x+y*y=r*r;

解出方程刚好有两个解,及为所求。

代码如下:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
using namespace std;

typedef long long LL;
const int N=21;
const LL II=1000000007;

double x,y,r,r2;
double xx0,yy0,xx1,yy1;

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        scanf("%lf%lf",&x,&y);
        r2=(x*x+y*y);
        double a,b,c;
        a=r2;
        b=r2*y;
        c=r2*r2/4-x*x*r2;
        yy0=(-b-sqrt(b*b-4*a*c))/2/a;
        yy1=(-b+sqrt(b*b-4*a*c))/2/a;
        /*/这个地方求解x不能用x*x+y*y=r2来算,应为不知道正负号
        xx0=sqrt(r2-yy0*yy0);
        xx1=sqrt(r2-yy1*yy1);*/
        if(fabs(x-0)<1e-7)
        {
            xx0=-sqrt(r2-yy0*yy0);
            xx1=sqrt(r2-yy1*yy1);
        }
        else
        {
            xx0=(-r2/2-yy0*y)/x;
            xx1=(-r2/2-yy1*y)/x;
        }
        printf("%.3lf %.3lf %.3lf %.3lf\n",xx0,yy0,xx1,yy1);
    }

    return 0;
}

解题报告转自:http://blog.csdn.net/xh_reventon/article/details/9231141


  1. 有两个重复的话结果是正确的,但解法不够严谨,后面重复的覆盖掉前面的,由于题目数据限制也比较严,所以能提交通过。已更新算法

  2. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。