Wednesday, July 16, 2008

Google Code Jam Beta 1 triangle Java

Code:

import java.io.*;
public class Triangle {
    double s1,s2,s3,area;
    
    Triangle(int x1,int y1,int x2,int y2,int x3,int y3){
        area=x1*(y3-y2)+x2*(y1-y3)+x3*(y2-y1);
        s1=distBetween(x1,y1,x2,y2);
        s2=distBetween(x1,y1,x3,y3);
        s3=distBetween(x2,y2,x3,y3);
    }
    
    double distBetween(int x1,int y1, int x2, int y2){
        double dist=Math.sqrt(Math.pow((x2-x1),2)+Math.pow((y2-y1),2));
        return dist;
    }
    
    void triType(){
        if(area==0) {
            System.out.println("not a triangle");
            return;
        }
        
        if(s1==s2 || s2==s3 || s1==s3){
            System.out.print("isosceles ");
        }else System.out.print("scalene ");
        
        if(Math.pow(s1, 2)== (Math.pow(s2, 2)+Math.pow(s3, 2)) ||
                Math.pow(s2, 2)==Math.pow(s1, 2)+Math.pow(s3, 2) ||
                    Math.pow(s3, 2)==Math.pow(s1, 2)+Math.pow(s2, 2))
                        System.out.println("right triangle"); 
        else if(Math.pow(s1, 2)>Math.pow(s2, 2)+Math.pow(s3, 2) ||
                Math.pow(s2, 2)>Math.pow(s1, 2)+Math.pow(s3, 2) ||
                    Math.pow(s3, 2)>Math.pow(s1, 2)+Math.pow(s2, 2))
                        System.out.println("obtuse triangle"); 
        else System.out.println("acute triangle");; 
            
    }
    
    
    public static void main(String[] in)throws IOException{
        File file=new File("./a2.txt");
        BufferedReader fileIn = new BufferedReader(new FileReader(file));
        String fileLine=fileIn.readLine(),inputs[];
        int iMax=Integer.parseInt(fileLine);
        for(int i=1;i<iMax+1;i++){
            fileLine=fileIn.readLine();
            inputs=fileLine.split(" ",6);
            int x1=Integer.parseInt(inputs[0]);
            int y1=Integer.parseInt(inputs[1]);
            int x2=Integer.parseInt(inputs[2]);
            int y2=Integer.parseInt(inputs[3]);
            int x3=Integer.parseInt(inputs[4]);
            int y3=Integer.parseInt(inputs[5]);
            System.out.print("Case #"+i+": ");
            new Triangle(x1,y1,x2,y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3 7 0 6 4
3 5 6 2 9 8
9 0 1 9 4 8
9 9 1 5 7 7
4 9 0 7 1 1
3 6 7 2 2 0
7 7 6 0 1 7
5 7 5 3 1 2
3 8 1 3 0 0
6 3 1 1 3 5
9 0 9 7 3 6
9 9 4 2 7 4
0 4 7 0 7 8
9 6 9 5 6 9
4 7 8 1 3 3
0 3 0 4 7 0
5 8 8 3 8 5

 

Output:

Case #1: isosceles obtuse triangle
Case #2: scalene acute triangle
Case #3: isosceles acute triangle
Case #4: scalene obtuse triangle
Case #5: scalene obtuse triangle
Case #6: isosceles obtuse triangle
Case #7: not a triangle
Case #8: not a triangle
Case #9: not a triangle
Case #10: not a triangle
Case #11: not a triangle
Case #12: isosceles acute triangle
Case #13: scalene right triangle
Case #14: isosceles right triangle
Case #15: scalene acute triangle
Case #16: not a triangle
Case #17: scalene obtuse triangle
Case #18: scalene obtuse triangle
Case #19: scalene acute triangle
Case #20: scalene obtuse triangle
Case #21: scalene obtuse triangle
Case #22: scalene obtuse triangle
Case #23: scalene obtuse triangle
Case #24: scalene acute triangle
Case #25: scalene obtuse triangle
Case #26: scalene acute triangle
Case #27: scalene obtuse triangle
Case #28: scalene obtuse triangle
Case #29: isosceles obtuse triangle
Case #30: scalene obtuse triangle
Case #31: scalene acute triangle
Case #32: scalene acute triangle
Case #33: scalene acute triangle
Case #34: scalene acute triangle
Case #35: scalene acute triangle
Case #36: scalene obtuse triangle
Case #37: scalene acute triangle
Case #38: scalene obtuse triangle
Case #39: scalene obtuse triangle
Case #40: scalene obtuse triangle
Case #41: scalene obtuse triangle
Case #42: scalene obtuse triangle
Case #43: not a triangle
Case #44: scalene obtuse triangle
Case #45: scalene obtuse triangle
Case #46: scalene obtuse triangle
Case #47: scalene acute triangle
Case #48: scalene right triangle
Case #49: scalene acute triangle
Case #50: scalene obtuse triangle
Case #51: scalene obtuse triangle
Case #52: scalene obtuse triangle
Case #53: scalene obtuse triangle
Case #54: scalene obtuse triangle
Case #55: scalene obtuse triangle
Case #56: scalene obtuse triangle
Case #57: scalene obtuse triangle
Case #58: scalene acute triangle
Case #59: scalene obtuse triangle
Case #60: scalene obtuse triangle
Case #61: scalene obtuse triangle
Case #62: scalene obtuse triangle
Case #63: scalene obtuse triangle
Case #64: scalene acute triangle
Case #65: scalene obtuse triangle
Case #66: scalene obtuse triangle
Case #67: not a triangle
Case #68: scalene obtuse triangle
Case #69: scalene obtuse triangle
Case #70: scalene obtuse triangle
Case #71: isosceles acute triangle
Case #72: scalene acute triangle
Case #73: scalene obtuse triangle
Case #74: scalene obtuse triangle
Case #75: scalene obtuse triangle
Case #76: scalene acute triangle
Case #77: scalene obtuse triangle
Case #78: scalene obtuse triangle
Case #79: not a triangle
Case #80: scalene acute triangle
Case #81: scalene obtuse triangle
Case #82: scalene acute triangle
Case #83: scalene obtuse triangle
Case #84: scalene acute triangle
Case #85: isosceles acute triangle
Case #86: scalene obtuse triangle
Case #87: scalene obtuse triangle
Case #88: scalene obtuse triangle
Case #89: scalene acute triangle
Case #90: scalene acute triangle
Case #91: scalene obtuse triangle
Case #92: scalene obtuse triangle
Case #93: scalene acute triangle
Case #94: scalene acute triangle
Case #95: scalene obtuse triangle
Case #96: isosceles acute triangle
Case #97: scalene obtuse triangle
Case #98: scalene obtuse triangle
Case #99: scalene obtuse triangle
Case #100: scalene obtuse triangle
blog comments powered by Disqus

Labels