Codeforces Round #239 (Div. 2) C. Triangle

来源:互联网 发布:授权回调域名出错 编辑:程序博客网 时间:2024/05/17 12:52
C. Triangle
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

There is a right triangle with legs of length a and b. Your task is to determine whether it is possible to locate the triangle on the plane in such a way that none of its sides is parallel to the coordinate axes. All the vertices must have integer coordinates. If there exists such a location, you have to output the appropriate coordinates of vertices.

Input

The first line contains two integers a, b (1 ≤ a, b ≤ 1000), separated by a single space.

Output

In the first line print either "YES" or "NO" (without the quotes) depending on whether the required location exists. If it does, print in the next three lines three pairs of integers — the coordinates of the triangle vertices, one pair per line. The coordinates must be integers, not exceeding 109 in their absolute value.

Sample test(s)
input
1 1
output
NO
input
5 5
output
YES2 15 5-2 4
input
5 10
output
YES-10 4-2 -21 2
这道题的大概意思就是:一个直角三角形(righttriangle),给出两条直角边的长度 然后判断是否存在和这个三角形全等的三角形,三条边都不和坐标轴平行的,并且三个点都在整数坐标上,如果存在的话先输出YES,然后再输出三角形三个点的坐标,不存在的话输出NO;

思路:我的方法就是,把输入的两个数看做是原点距离原点在x轴上的坐标和y轴上的坐标,所以两个点的坐标就分别为B(x,0),C(0,y),A(0,0),以原点为中心旋转这个三角形,旋转90°就可以(因为旋转90度之后,在旋转的话就等于是重复了),如果在0°到90°之间存在这个三角形,那么就存在,否则的话就不存在,还有就是最好不要开方,这样的话会丢失精度,但是提交的话可能是让过的。

不是三角形直角顶点的另外两个点,只要有一个在格点上,那么另外一个肯定也在格点上。

因为这道题的的数据比较小,可以用枚举的方法,用4重循环

#include<iostream>#include<cstdio>#include<cmath>using namespace std;bool fun(int x1,int y1,int x2,int y2)//传入的是两个点的坐标,只要两个点的横坐标不相等、纵坐标不相等,就证明这两个点的连线和坐标轴不平行{    if(x1!=x2 && y1!=y2)        return true;    else        return false;}int main(){    int a,b;    int x1,x2,x3,y1,y2,y3;    int i,j,p,q;    scanf("%d%d",&x2,&y3);    x1 = 0;    y1 = 0;    y2 = 0;    x3 = 0;    //    int max = x2>y3?x2:y3;    int min = x2+y3-max;    for(i=max;i>0;i--)    {        for(j=0;j<=max;j++)        {            if(i*i+j*j == max*max )            {                x2 = i;                y2 = j;                for(p = min;p>0;p--)                {                    for(q = 0;q<=min;q++)                    {                        if(q*q+p*p == min*min)//这个点到原点的距离和其中短的那条边到远点的距离相等                        {                            x3 = -q;//因为是直角三角形,所以不是90度的那个点在整数的格子上,那么另外一个点也在格子上。                            y3 = p;                            if(fun(x1,y1,x2,y2) && fun(x3,y3,x2,y2)&&fun(x1,y1,x3,y3))//判断三个点是否有其中两个点的连线和坐标轴平行                            {                                if((y3-y2)*(y3-y2)+(x3-x2)*(x3-x2) == min*min+max*max)//斜边的长度和原来三角形斜边的长度相等                                {                                    printf("YES\n");                                    printf("%d %d\n",x1,y1);                                    printf("%d %d\n",x2,y2);                                    printf("%d %d\n",x3,y3);                                                                return 0;                                }                            }                        }                    }                }            }        }    }    printf("NO");    return 0;}

0 0