ACM: Problem 7371

Problem 7371 Triangle


/*
Problem 7371

Sample Input
3 4 5
4 3 5
3 4 6
4 6 3
39 52 65
25 60 65


Sample Output
YES
NO
NO
*/


import java.io.*;
import java.util.*;

class Main
{
    static String ReadLn (int maxLg)  // utility function to read from stdin
    {
        byte lin[] = new byte [maxLg];
        int lg = 0, car = -1;
        String line = "";

        try
        {
            while (lg < maxLg)
            {
                car = System.in.read();
                if ((car < 0) || (car == '\n')) break;
                lin [lg++] += car;
            }
        }
        catch (IOException e)
        {
            return (null);
        }

        if ((car < 0) && (lg == 0)) return (null);  // eof
        return (new String (lin, 0, lg));
    }

    public static void main (String args[])  // entry point from OS
    {
        Main myWork = new Main();  // create a dinamic instance
        myWork.Begin();            // the true entry point
    }

	void Begin()
	{
		String input;
		StringTokenizer idata;
		double[] tri1 = new double[3];
		double[] tri2 = new double[3];
		String yesno = "YES";
		
		while ((input = Main.ReadLn (255)) != null)
		{
			yesno = "YES";
			idata = new StringTokenizer (input);	
				
			if(!idata.hasMoreElements())//handle blank line at the end of parsing
				break;
			
			//read first triangle		
			for(int i = 0; i < 3; i++){
				tri1[i]= Integer.parseInt (idata.nextToken()); 
			}
			
			input = Main.ReadLn(255);
			idata = new StringTokenizer (input);
			
			//read second triangle
			for(int i = 0; i < 3; i++){
				if(!idata.hasMoreElements())//handle blank line at the end of parsing
					break;
				tri2[i]= Integer.parseInt (idata.nextToken()); 
			}
			
			Arrays.sort(tri1);
			Arrays.sort(tri2);
			
			for(int i = 0; i < 3; i++){
				//System.out.println(tri2[i] + " " + tri1[i]);
				if(tri1[i] != tri2[i]){
					yesno = "NO";
				}
			}
			if(yesno.equals("YES")){
				double hyp = Math.sqrt((tri1[0]*tri1[0]) + (tri2[1]*tri2[1]));
				if(hyp != tri1[2]){
					yesno = "NO";
				}

			}
			System.out.println(yesno);		
		}
		
	}
}

Leave a Reply