CodeForces 407A Triangle

题目地址: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();
	}

}


你可能感兴趣的:(CodeForces 407A Triangle)