Codeforces Round #239 (Div. 2) C. Triangle(简单数学+暴力)

1、http://codeforces.com/contest/408/problem/C

2、题目大意:

给出一个直角三角形的两条边长a、b,看能否将这个三角形放到一个二维平面内,使得三条边都不能平行于坐标轴,如果能输出三个点的坐标,否则输出NO

题目不难,但是写的时候忘记判断直角三角形了,没有一遍AC,好在及时发现错误,把题目AC了。。。

3、思路:

通过画图很容易发现,两条直角边也就是ab分别是另外两个平行于坐标轴的直角三角形的斜边,那么我们此时就可以暴力做了,看能不能找到这样的两条斜边,如果有一个找不到,那么就是NO,如果能找到就存起来,然后判断最终的三角形是否是直角三角形以及最后一条边是否是平行于坐标轴的

4、题目:

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
5、AC代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
int sai[1005],saj[1005],sbi[1005],sbj[1005];
int main()
{
    int a,b;
    while(scanf("%d%d",&a,&b)!=EOF)
    {
        int flag1=0,flag2=0,ka=0,kb=0,kx1,kx2,ky1,ky2;
        for(int i=1; i<=a; i++)
        {
            for(int j=1; j<=a; j++)
            {
                if(i*i+j*j==a*a)
                {
                    flag1=1;
                    sai[ka]=i;
                    saj[ka]=j;
                    ka++;
                    if(i!=j)
                    {
                        sai[ka]=j;
                        saj[ka]=i;
                        ka++;
                    }
                }
                if(i*i+j*j>a*a)
                    break;
            }
        }

        for(int i=1; i<=b; i++)
        {
            for(int j=1; j<=b; j++)
            {
                if(i*i+j*j==b*b)
                {
                    flag2=1;
                    sbi[kb]=i;
                    sbj[kb]=j;
                    kb++;
                    if(i!=j)
                    {
                        sbi[kb]=j;
                        sbj[kb]=i;
                        kb++;
                    }
                }
                if(i*i+j*j>b*b)
                    break;
            }
        }
        if(flag1==0 || flag2==0)
        {
            printf("NO\n");
        }
        else
        {
            int flag=0;
            int ansi,ansj;
            for(int i=0; i<ka; i++)
            {
                kx1=-saj[i];
                ky1=sai[i];
                for(int j=0; j<kb; j++)
                {
                    kx2=sbi[j];
                    ky2=sbj[j];
                    if((ky1!=ky2))
                    {
                        int tmp1=(kx2-kx1)*(kx2-kx1)+(ky2-ky1)*(ky2-ky1);
                        int tmp2=a*a+b*b;
                        if(tmp1==tmp2)
                        {
                            flag=1;
                            ansi=i;
                            ansj=j;
                            break;
                        }
                    }
                }
                if(flag==1)
                    break;
            }
            if(flag==1)
            {
                printf("YES\n");
                printf("0 0\n");
                printf("%d %d\n",-saj[ansi],sai[ansi]);
                printf("%d %d\n",sbi[ansj],sbj[ansj]);
            }
            else
            {
                printf("NO\n");
            }
        }
    }
    return 0;
}
/*
1 1
5 5
5 10
*/



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值