There is a right triangle with legs of lengthaand 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.
The first line contains two integersa, b(1 ≤ a, b ≤ 1000), separated by a single space.
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 exceeding109in their absolute value.
1 1
NO
5 5
YES 2 1 5 5 -2 4
5 10
YES -10 4 -2 -2 1 2
解题报告:数学题。题意是找出一个边长为a,b的直角三角形使其不能平于坐标轴,如果存在输出YES并且输出三点的坐标,不存在则输出NO。思路是固定直角坐标使其为(0,0)然后顺时针旋转90度。用四个for循环找出合适的即可
参考代码:
#include <iostream>
using namespace std;
int main()
{
int i,j,i2,j2,n1,n2,s,a,b;
while(cin>>a>>b)
{
bool flag=false;
s=a*a+b*b;
for(i=-1;i>=-a;i--)
{
for(j=1;j<=a;j++)
{
if((i*i+j*j==a*a))
{
for(i2=1;i2<=b;i2++)
{
for(j2=1;j2<=b;j2++)
{
if(j2*j2+i2*i2==b*b)
{
if(((i2-i)*(i2-i)+(j2-j)*(j2-j)==a*a+b*b)&&(i2!=i)&&j2!=j)
{
flag=true;
break;
}
}
}
if(flag)
break;
}
if(flag)
break;
}
}
if(flag)
break;
}
if(flag)
{
cout<<"YES"<<endl;
cout<<"0 0"<<endl;
cout<<i<<" "<<j<<endl;
cout<<i2<<" "<<j2<<endl;
}
else{
cout<<"NO"<<endl;
}
}
return 0;
}