Triangle(暴搜)

A. 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

  

      题意:

      给出 A(1 ~ 1000),B (1 ~ 1000),代表直接三角形的两条边。问能否构成一个直角三角形,这个三角形的任意一条边都不与坐标系平行。能的话则输出这个三个点的坐标。

 

      思路:

      暴搜。设一个不动顶点(0,0),再寻找另外两个坐标即可,另外两个坐标到(0,0)这个点的距离刚好是边长 a,b,可以用 sqrt(x * x + y * y)求得,所以这样子就可以求出另外两点。但是求出来之后还要判断

这两个点构成的两条边是否为直角,并且斜边是否与 X 轴平行。

 

      AC:

#include <cstdio>
#include <algorithm>
using namespace std;

bool test (int a,int b,int r) {
        if(a * a + b * b == r * r) return true;
        return false;
}

int main() {
        int x1, y1, x2, y2, temp = 0;
        int a, b;

        scanf("%d%d",&a,&b);

        for (x1 = 1; x1 <= a; ++x1) {
            for (y1 = 1; y1 <= a; ++y1) {

                    if(test(x1,y1,a)) {
                            int x11 = -x1;

                            for (x2 = 1; x2 <= b; ++x2) {
                                    for (y2 = 1; y2 <= b; ++y2) {
                                            if(test(x2,y2,b) &&
                                               x11 * x2 + y1 * y2 == 0 &&
                                               y1 != y2) {
                                                    printf("YES\n");
                                                    printf("%d %d\n",x11, y1);
                                                    printf("%d %d\n",x2, y2);
                                                    printf("0 0\n");
                                                    temp = 1;
                                                    break;
                                            }
                                    }
                                    if(temp) break;
                            }
                    }
                    if(temp) break;
            }
            if(temp) break;
        }

        if(!temp) printf("NO\n");
        return 0;
}

 

 

 

你可能感兴趣的:(RIA)