A. Srdce and Triangle--“今日头条杯”首届湖北省大学程序设计竞赛(网络同步赛)

如下图这是“今日头条杯”首届湖北省大学程序设计竞赛的第一题,作为赛后补题

 

题目描述:链接点此

这套题的github地址(里面包含了数据,题解,现场排名):点此

 

 


Let  be a regualr triangle, and D is a point in the triangle. Given the angle of . Then let AD, CD and BD form a new triangle, what is the size of the three angles?

输入描述:

Input contains multiple cases, please process to the end of input.

For each line in the input, contains three integers, $\alpha, \beta, \gamma$, which are the size of the angel , and in degree.

输出描述:

For each line of input, output one line with three numbers in ascending order, the three angles in the new triangle, your answer will be considered as correct if and only if the relative or absolute error is less than 

, If the new triangle cannot be formed, output -1 -1 -1 instead. 

 

题目解析:就是一个等边三角形,然后在等边三角形内有一点D,已知  and  ,求以AD,BD,CD为边构成的三角形的内角。

 

现场列了三个方程:

但是很难受,解方程解了3个小时,并没有算出来(可能是我数学太弱了),后来想二分,二分角DAB,但是现场wa了,回头补上来,

 

现在说一种十分简单的方法:

 

这是不是一个代数题,是一道解题目,我们把三角形ABC逆时针旋转60度,如下图:

就会出现以abc为边构成的三角形PP'C,由于ΔAPP'是等边三角形,所以可以求出三个角分别为  and  减去60度,这是个水题啊。(ORZ,卡了4个小时)

 

/*
    data:2018.04.22
    author:gsw
    link:https://www.nowcoder.com/acm/contest/104#question
    tip:武大校赛--补题
*/
#define IO ios::sync_with_stdio(false);
#define ll long long

#include<iostream>
#include<string.h>
#include<math.h>
#include<stdio.h>
#include<vector>
#include<algorithm>

using namespace std;

int main()
{
    int a,b,c;
    while(~scanf("%d%d%d",&a,&b,&c))
    {
        int ans[3];
        ans[0]=a-60;
        ans[1]=b-60;
        ans[2]=c-60;
        sort(ans,ans+3);
        printf("%d %d %d\n",ans[0],ans[1],ans[2]);
    }
}

 

 

 

 

下面会有二分的做法:

对角DAB进行二分,我试了好几次,把所有的优化都做了,要把esp调到1e-12才可以过

 

然后注意几点优化:把所有的变量定义放在外面,这个优化挺重要的。

 

/*
    data:2018.04.22
    author:gsw
    link:https://www.nowcoder.com/acm/contest/104#question
    tip:武大校赛--补题
*/
#define IO ios::sync_with_stdio(false);
#define ll long long

#include<iostream>
#include<string.h>
#include<math.h>
#include<stdio.h>
#include<vector>
#include<algorithm>
int a,b,c;
using namespace std;

const double expp=1e-12;
const double pi=3.14159265358;
const double g3=sqrt(3)/2;
double cosc,x,y,ad,cd,bd,cos_c,th1,th2,mid,ans[3];
inline bool judge(double th1)
{
    th2=180-a-th1;
    th1=th1/180*pi;th2=th2/180*pi;
    th1=tan(th1);th2=tan(th2);
    x=th2/(th2+th1);
    y=x*th1;
    //cout<<"x= "<<x<<" "<<y<<endl;
    ad=x*x+y*y;
    cd=(x-0.5)*(x-0.5)+(y-g3)*(y-g3);
    cos_c=(ad+cd-1)/(2*sqrt(ad*cd));
    //cout<<cos_c<<" "<<cosc<<endl;
    if(cos_c>cosc)return 1;
    else return 0;
}
inline double ef(double l,double r)
{
    while(r-l>expp)
    {
        mid=(l+r)/2;
        if(judge(mid))l=mid;
        else r=mid;
        //cout<<l<<" "<<r<<endl;
    }
    return l;
}
int main()
{
    //freopen("temin.txt","r",stdin);
    //freopen("temout.txt","w",stdout);

    while(~scanf("%d%d%d",&a,&b,&c))
    {
        cosc=cos((double)c/180*pi);
        th1=ef(0,min(180-a,60));
        th2=180-a-th1;
        th1=th1/180*pi;th2=th2/180*pi;
        th1=tan(th1);th2=tan(th2);
        x=th2/(th2+th1);
        y=x*th1;//th2*th1/(th2+th1);
        //cout<<x<<" "<<y<<endl;
        ad=x*x+y*y;
        cd=(x-0.5)*(x-0.5)+(y-g3)*(y-g3);
        bd=(x-1)*(x-1)+y*y;
        //cout<<ad<<" "<<cd<<" "<<bd<<endl;
        ans[0]=acos((ad+cd-bd)/(2*sqrt(ad*cd)))/pi*180;
        ans[1]=acos((ad+bd-cd)/(2*sqrt(ad*bd)))/pi*180;
        ans[2]=acos((bd+cd-ad)/(2*sqrt(bd*cd)))/pi*180;
        sort(ans,ans+3);
        printf("%f %f %f\n",ans[0],ans[1],ans[2]);
    }
}

 

 

再此跟新::

 

这个优化还是不到位啊,有时t,有时A。时间总是差个几毫秒

 

posted @ 2018-04-24 10:29  fantastic123  阅读(362)  评论(0编辑  收藏  举报