題目地址:http://codeforces.com/problemset/problem/407/A
題意: 給你一個直角三角形的兩條直角邊長度a,b,判斷該三角形是否滿足以下約束:1,座標都是整數,2,沒有一條邊和座標軸平行。若有,輸出“YES”和座標,若無,輸出“NO”
思路:假定一個點(x1,y1)在座標原點,通過搜索由長度a確定的座標區域,可以得到(x3,y3)的可能解。通過,直角邊垂直關係和長度約束,可以算出(x2,y2)=(-b*y3/a,b*x3/a)或者(b*y3/a,-b*x3/a)。再驗證約束是否滿足。
package Triangle;
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a = in.nextInt(),b = in.nextInt();
double x3=0,y3=0;
double x2=0,y2=0;
int flag = 0;
for(int i=-a;i<=a;i++)
{
if(flag == 1) break;
for(int j=-a;j<=a;j++)
{
x3=i;
y3=j;
if(x3==0||y3==0)
continue;
if(x3*x3+y3*y3==a*a)
{
x2 = ((double)b)*y3/a;
y2 = -((double)b)*x3/a;
if(Math.floor(x2)==x2&&Math.floor(y2)==y2)
{
int tempx2=(int)x2;
int tempy2=(int)y2;
if(tempx2!=x3&&tempy2!=y3)
{
flag = 1;
break;
}
}
x2 = -((double)b)*y3/a;
y2 = ((double)b)*x3/a;
if(Math.floor(x2)==x2&&Math.floor(y2)==y2)
{
int tempx2=(int)x2;
int tempy2=(int)y2;
if(tempx2!=x3&&tempy2!=y3)
{
flag = 1;
break;
}
}
}
}
}
if(flag == 1)
{
System.out.println("YES");
System.out.println("0 "+"0");
System.out.println((int)x2+" "+(int)y2);
System.out.println((int)x3+" "+(int)y3);
}
else
{
System.out.println("NO");
}
in.close();
}
}