现在的位置: 首页 > 综合 > 正文

codeores–C. Triangle–3.30

2014年10月30日 ⁄ 综合 ⁄ 共 1352字 ⁄ 字号 评论关闭
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
YES
2 1
5 5
-2 4
Input
5 10
Output
YES
-10 4
-2 -2
1 2
题目描述,直角三角行,给出两个直角边的长度,要求求出一组坐标,使得每一个点都在整数点上,且不垂直与x轴y轴
假设直角点在(0,0)上,那么其他两个点都可以穷举获得 注意 y1 != y2 防止第三条边不能平行于x轴
#include <stdio.h>
#include <math.h>
int main()
{
    int i ,j , a , b ,x1 = 0 , y1 = 0 , x2 = 0 , y2 = 0 ;
    scanf("%d %d", &a, &b);
    for(i = 1 ; i < a ; i++)
    {
        x1 = i ;
        y1 = sqrt(a*a-x1*x1);
        if( x1*x1 + y1*y1 == a*a)
        {
            for(j = -1 ; j > -b ; j--)
            {
                x2 = j ;
                y2 = sqrt( b*b-x2*x2 );
                if(x2*x2 + y2*y2 == b*b && x1*x2 + y1*y2 == 0 && y1 != y2)
                    break;
            }
            if(j > -b)
                break;
        }
    }
    if(x2*x2 + y2*y2 == b*b && x1*x1 + y1*y1 == a*a && x1*x2 + y1*y2 == 0)
    {
        printf("YES\n");
        printf("0 0\n");
        printf("%d %d\n", x1, y1);
        printf("%d %d\n", x2, y2);
    }
    else
        printf("NO\n");
    return 0;
}

抱歉!评论已关闭.