Pages

Showing posts with label Geometry. Show all posts
Showing posts with label Geometry. Show all posts

Tuesday, 15 September 2015

CodeEval - Compare Points - Easy

import sys

test_cases = open(sys.argv[1], 'r')
for test in test_cases:
    cord = map(int, test.split())
    sol = ""
    if cord[1] > cord[3]:
        sol += "S"
    elif cord[1] < cord[3]:
        sol += "N"
    if cord[0] > cord[2]:
        sol += "W"
    elif cord[0] < cord[2]:
        sol += "E"
    if cord[0] == cord[2] and cord[1] == cord[3]:
        sol += "here"
    print sol

test_cases.close()

Friday, 2 May 2014

CodeEval - Find a Square - Moderate

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;


public class Main {
    public static void main (String[] args) throws FileNotFoundException, IOException {

    BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
    StringBuffer sb=new StringBuffer();
    String line;
    while ((line = in.readLine()) != null) {
        Point []points=new Point[4];
        StringTokenizer st=new StringTokenizer(line," ");
        for(int i=0;i<4;i++){
            points[i]=parsePoint(st.nextToken());
        }
        Point center=new Point(0,0);
        for(int i=0;i<4;i++){
            center.x+=points[i].x;
            center.y+=points[i].y;
        }
        center.x/=4;
        center.y/=4;
        boolean flag=true;
        double dist=eculDist(center, points[0]);
        for(int i=1;i<4;i++){
            double temp=eculDist(center, points[i]);
            if(temp!=dist){
                 sb.append(false).append("\n");
                 flag=false;
                 break;
            }
        }
        if(flag){
            int counter=0;
            for(int i=1;i<4;i++){
                if(dotProd(difPoint(points[0], center), difPoint(points[i], center))==0)
                        counter++;
            }
            if(counter==2){
                sb.append(true).append("\n");
            }
            else{
                sb.append(false).append("\n");
            }
        }
       
    }
    System.out.print(sb);
  }
   
    static double eculDist(Point p1,Point p2){
        return Math.sqrt(((p1.x-p2.x)*(p1.x-p2.x))+((p1.y-p2.y)*(p1.y-p2.y)));
    }
   
    static double dotProd(Point p1,Point p2){
        return p1.x*p2.x+p1.y*p2.y;
    }
   
    static Point difPoint(Point p1,Point p2){
        return new Point(p1.x-p2.x,p1.y-p2.y);
    }
   
    static Point parsePoint(String string){
        StringBuilder str=new StringBuilder(string);
        str.deleteCharAt(0);
        if(str.charAt(str.length()-1)==',')
            str.deleteCharAt(str.length()-1);
        str.deleteCharAt(str.length()-1);
        StringTokenizer st2=new StringTokenizer(str.toString(),",");
        return new Point(Integer.parseInt(st2.nextToken()),Integer.parseInt(st2.nextToken()));
    }
}

class Point{
    double x;
    double y;

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }
   
   
}

CodeEval - Point in Circle - Moderate

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;


public class Main {
    public static void main (String[] args) throws FileNotFoundException, IOException {

    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    StringBuffer sb=new StringBuffer();
    String line;
    while ((line = in.readLine()) != null) {
        StringTokenizer st=new StringTokenizer(line,";");
        StringBuilder str=new StringBuilder(st.nextToken());
        str.delete(0, 9);
        str.deleteCharAt(str.length()-1);
        StringTokenizer st2=new StringTokenizer(str.toString(),",");
        double centerX=Double.parseDouble(st2.nextToken());
        double centerY=Double.parseDouble(st2.nextToken().trim());
       
        str=new StringBuilder(st.nextToken());
        str.delete(0, 9);
        double radius=Double.parseDouble(str.toString());
       
        str=new StringBuilder(st.nextToken());
        str.delete(0, 9);
        str.deleteCharAt(str.length()-1);
        st2=new StringTokenizer(str.toString(),",");
        double pX=Double.parseDouble(st2.nextToken());
        double pY=Double.parseDouble(st2.nextToken().trim());
        sb.append(!(eculDist(pX, pY, centerX, centerY)>radius)).append("\n");
    }
    System.out.print(sb);
  }
   
    static double eculDist(double x1,double y1,double x2,double y2){
        return Math.sqrt(((x1-x2)*(x1-x2))+((y1-y2)*(y1-y2)));
    }
}

Sunday, 22 December 2013

UVA - 12704 - Little Masters

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>

int main()
{
    int n=0;
    scanf("%d",&n);
    while(n--){
        float x=0,y=0,r=0;
        scanf("%f %f %f",&x,&y,&r);
        double s=sqrt(x*x+y*y);
        printf("%.2f %.2f\n",r-s,r+s);
    }
    return 0;
}

Saturday, 13 July 2013

UVA - 12611 - Beautiful Flag

#include <stdio.h>

int main()
{
    int cases,i=1, r;
    scanf("%d",&cases);
    while(cases--){
        scanf("%lf",&r);
        printf("Case %d:\n",i);
        printf("%.0lf %.0lf\n",-2.25*r,1.5*r);
        printf("%.0lf %.0lf\n",2.75*r,1.5*r);
        printf("%.0lf %.0lf\n",2.75*r,-1.5*r);
        printf("%.0lf %.0lf\n",-2.25*r,-1.5*r);
        i++;
    }
    return 0;
}

Friday, 25 January 2013

UVA - 477 - Points in Figures: Rectangles and Circles

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb = new StringBuffer();
        String m = "";
        LinkedList<Rectangle> listrec=new LinkedList<Rectangle>();
        LinkedList<Circle> listcirc=new LinkedList<Circle>();
        LinkedList<Integer> typ=new LinkedList<Integer>();
        while(true){
                m=br.readLine();
                if("*".equals(m))
                    break;
                StringTokenizer st=new StringTokenizer(m);
                String type=st.nextToken();
                if(type.charAt(0)=='r'){
                    listrec.add(new Rectangle(Double.parseDouble(st.nextToken()),
                                Double.parseDouble(st.nextToken()),
                                Double.parseDouble(st.nextToken()),
                                Double.parseDouble(st.nextToken())));
                    typ.add(0);
                }else{
                    listcirc.add(new Circle(Double.parseDouble(st.nextToken()),
                            Double.parseDouble(st.nextToken()),
                            Double.parseDouble(st.nextToken())));
                    typ.add(1);
                }
        }
        Rectangle[]rects=new Rectangle[listrec.size()];
        for(int i=0;i<rects.length;i++){
            rects[i]=listrec.remove();
        }
        Circle[]circs=new Circle[listcirc.size()];
        for(int i=0;i<circs.length;i++){
            circs[i]=listcirc.remove();
        }
        int[]types=new int[typ.size()];
        for(int i=0;i<types.length;i++){
            types[i]=typ.remove();
        }
        int cases=1;
        while(true){
            Double x,y;
            StringTokenizer st=new StringTokenizer(br.readLine());
            x=Double.parseDouble(st.nextToken());
            y=Double.parseDouble(st.nextToken());
            if(x==9999.9 &&y==9999.9){
                break;
            }
            Point p=new Point(x, y);
            boolean flag=false;
            for(int i=0,j=0,z=0;i<types.length;i++){
                if(types[i]==0){
                    if(pointInRect(p, rects[j])){
                        String str=String.format("Point %d is contained in figure %d\n", cases,(i+1));
                        sb.append(str);
                        flag=true;
                    }
                    j++;
                }else{
                   if(pointInCircle(p, circs[z])){
                        String str=String.format("Point %d is contained in figure %d\n", cases,(i+1));
                        sb.append(str);
                        flag=true;
                    }
                    z++;
                }
            }
            if(!flag){
                String str=String.format("Point %d is not contained in any figure\n", cases);
                sb.append(str);
            }
            cases++;
        }
        System.out.print(sb);
    }
   
    static boolean pointInRect(Point p,Rectangle x){
        return x.getX1()<p.getX()&&x.getX2()>p.getX()
                &&x.getY1()<p.getY()&&x.getY2()>p.getY();
    }
   
    static boolean pointInCircle(Point p,Circle x){
       double tempX=(p.getX()-x.getCenterX());
       tempX*=tempX;
       double tempY=(p.getY()-x.getCenterY());
       tempY*=tempY;
       return (x.getRadius()>Math.abs(Math.sqrt(tempX+tempY)));
    }
   
}
class Point {
    private double x;
    private double y;

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }
   
}
class Rectangle{
    private double x1;
    private double y1;
    private double x2;
    private double y2;

    public Rectangle(double x1, double y1, double x2, double y2) {
        this.x1 = Math.min(x1, x2);
        this.y1 = Math.min(y1, y2);
        this.x2 = Math.max(x1, x2);
        this.y2 = Math.max(y1, y2);
    }
  
    public double getX1() {
        return x1;
    }

    public void setX1(double x1) {
        this.x1 = x1;
    }

    public double getX2() {
        return x2;
    }

    public void setX2(double x2) {
        this.x2 = x2;
    }

    public double getY1() {
        return y1;
    }

    public void setY1(double y1) {
        this.y1 = y1;
    }

    public double getY2() {
        return y2;
    }

    public void setY2(double y2) {
        this.y2 = y2;
    }
  
    public double getWidth(){
        return x2-x1;
    }
  
    public double getHeight(){
        return y2-y1;
    }
  
    public double getArea(){
        return getWidth()*getHeight();
    }
  
    public double getPeri(){
        return 2*(getWidth()+getHeight());
    }
}class Circle{
    private double centerX;
    private double centerY;
    private double radius;

    public Circle(double centerX, double centerY, double radius) {
        this.centerX = centerX;
        this.centerY = centerY;
        this.radius = radius;
    }

    public double getCenterX() {
        return centerX;
    }

    public void setCenterX(double centerX) {
        this.centerX = centerX;
    }

    public double getCenterY() {
        return centerY;
    }

    public void setCenterY(double centerY) {
        this.centerY = centerY;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

}

Wednesday, 23 January 2013

UVA - 476 - Points in Figures: Rectangles

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb = new StringBuffer();
        String m = "";
        LinkedList<Rectangle> list=new LinkedList<Rectangle>();
        while(true){
                m=br.readLine();
                if("*".equals(m))
                    break;
                StringTokenizer st=new StringTokenizer(m);
                st.nextToken();
                list.add(new Rectangle(Double.parseDouble(st.nextToken()),
                            Double.parseDouble(st.nextToken()),
                            Double.parseDouble(st.nextToken()),
                            Double.parseDouble(st.nextToken())));
        }
        Rectangle[]rects=new Rectangle[list.size()];
        for(int i=0;i<rects.length;i++){
            rects[i]=list.remove();
        }
        int cases=1;
        while(true){
            Double x,y;
            StringTokenizer st=new StringTokenizer(br.readLine());
            x=Double.parseDouble(st.nextToken());
            y=Double.parseDouble(st.nextToken());
            if(x==9999.9 &&y==9999.9){
                break;
            }
            Point p=new Point(x, y);
            boolean flag=false;
            for(int i=0;i<rects.length;i++){
                if(pointInRect(p, rects[i])){
                    String str=String.format("Point %d is contained in figure %d\n", cases,(i+1));
                    sb.append(str);
                    flag=true;
                }
            }
            if(!flag){
                String str=String.format("Point %d is not contained in any figure\n", cases);
                sb.append(str);
            }
            cases++;
        }
        System.out.print(sb);
    }
   
    static boolean pointInRect(Point p,Rectangle x){
        return x.getX1()<p.getX()&&x.getX2()>p.getX()
                &&x.getY1()<p.getY()&&x.getY2()>p.getY();
    }
   
}class Point {
    private double x;
    private double y;

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }
   
}
class Rectangle{
    private double x1;
    private double y1;
    private double x2;
    private double y2;

    public Rectangle(double x1, double y1, double x2, double y2) {
        this.x1 = Math.min(x1, x2);
        this.y1 = Math.min(y1, y2);
        this.x2 = Math.max(x1, x2);
        this.y2 = Math.max(y1, y2);
    }
  
    public double getX1() {
        return x1;
    }

    public void setX1(double x1) {
        this.x1 = x1;
    }

    public double getX2() {
        return x2;
    }

    public void setX2(double x2) {
        this.x2 = x2;
    }

    public double getY1() {
        return y1;
    }

    public void setY1(double y1) {
        this.y1 = y1;
    }

    public double getY2() {
        return y2;
    }

    public void setY2(double y2) {
        this.y2 = y2;
    }
  
    public double getWidth(){
        return x2-x1;
    }
  
    public double getHeight(){
        return y2-y1;
    }
  
    public double getArea(){
        return getWidth()*getHeight();
    }
  
    public double getPeri(){
        return 2*(getWidth()+getHeight());
    }
}

Thursday, 17 January 2013

UVA - 10167 - Birthday Cake

/*You don't have to brute force A from -500 to 500 and B from -500 to 500.
You can brute force A from 0 to 500 and B from -500 to 500  as if (-500,-500) is the same as (500,500) .
*/
#include<stdio.h>
#include<string.h>
#define MAX 101

int x[MAX],y[MAX];

int ans(int A,int B,int n){
    int i,pos=0,neg=0;
    for(i=0;i<2*n;i++){
        int temp=A*x[i]+B*y[i];
        if(temp>0){
            pos++;
        }
        else if(temp<0){
            neg++;
        }else{
            return 0;
        }
    }
    if(pos==neg)
        return 1;
    return 0;
}

int main()
{
    int i,j,n;
    while(1){
        scanf("%d",&n);
        if(n==0)
            break;
        for(i=0;i<2*n;i++){
            scanf("%d %d",&x[i],&y[i]);
        }
        int temp=-1,tempA=-1,tempB=-1;
        for(i=0;i<501;i++){
            for(j=-500;j<501;j++){
                temp=ans(i,j,n);
                if(temp==1){
                    tempA=i;
                    tempB=j;
                    break;
                }
            }
            if(temp==1){
                break;
            }
        }
        printf("%d %d\n",tempA,tempB);
    }
    return 0;
}

Wednesday, 16 January 2013

UVA - 270 - Lining Up

#include<stdio.h>
#include<string.h>
#define MAX 701

int coolinear(int x1,int y1,int x2,int y2,int x3,int y3){
    return (y1 - y2) * (x1 - x3) == (x1 - x2) * (y1 - y3);
}

int main()
{
    int i,ic,n ,j, z, counter, points,max;
    int x[MAX], y[MAX], arr[MAX][MAX], temp[MAX] ;
    char str[150];
    gets(str);
    sscanf(str, "%d", &n);
    gets(str);
    for(ic=0;ic<n;ic++){
        if(ic>0)
            printf("\n");
        points = 0;
        while(gets(str) != NULL){
            if(str[0] == '\0')
                break;
            sscanf(str, "%d%d", &x[points], &y[points]);
            points++;
        }
        memset(arr, -1, sizeof(arr));
        max = 0;
        for(i = 0; i < points; i ++)
            for(j = i + 1; j < points; j ++)
                if(arr[i][j]){
                    counter = 0;
                    for(z = 0; z < points; z++)
                        if(coolinear(x[i],y[i],x[j],y[j],x[z],y[z]))
                            temp[counter++] = z;
                    if(counter > max)
                        max = counter;
                    int l,m;
                    for(l = 0; l < counter; l ++)
                        for(m = l + 1; m < counter; m ++){
                            arr[temp[l]][temp[m]] = 0;
                            arr[temp[m]][temp[l]] = 0;
                        }
                }
        printf("%d\n", max);
    }
    return 0;
}

UVA - 184 - Laser Lines

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb=new StringBuffer();
            String m="";
            LinkedList<Point> list=new  LinkedList<Point>();
            while(true){
                m=br.readLine();
                if(m.equals("0 0")){
                    break;
                }
                StringTokenizer st=new StringTokenizer(m);
                int tokens=st.countTokens()/2;
                int lastX=0,lastY=0;
                for(int j=0;j<tokens;j++){
                    if(j==tokens-1){
                        lastX=Integer.parseInt(st.nextToken());
                        lastY=Integer.parseInt(st.nextToken());
                        if(!(lastX==0&&lastY==0)){
                            list.add(new Point(lastX,lastY));
                        } 
                    }else{
                        list.add(new Point(Integer.parseInt(st.nextToken()),
                                Integer.parseInt(st.nextToken())));
                    }
                }
                if(lastX!=0||lastY!=0){
                    continue;
                }
                Point[]arr=new Point[list.size()];
                for(int i=0;i<arr.length;i++){
                    arr[i]=list.remove();
                }
                Arrays.sort(arr);
                boolean getit=false;
                boolean checked[][]=new boolean[arr.length][arr.length];
                for(int i=0;i<arr.length;i++){  
                   for(int j=i+1;j<arr.length;j++){
                       if(checked[i][j]){
                           continue;
                       }
                        LinkedList<Point> temp=new LinkedList<Point>();
                        LinkedList<Integer> zs=new LinkedList<Integer>();
                        temp.add(arr[i]);
                        temp.add(arr[j]);
                        boolean tempL[]=new boolean[arr.length];
                        for(int z=j+1;z<arr.length;z++){
                            if(checked[i][z]||checked[j][z]){
                                continue;
                            }
                            if(coolinear(arr[i],arr[j], arr[z])){
                                temp.add(arr[z]);
                                checked[i][j]=true;
                                checked[j][z]=true;
                                checked[i][z]=true;
                                zs.add(z);
                            }
                        }
                        if(temp.size()>2){
                            if(!getit){
                                 sb.append("The following lines were found: \n");
                            }
                            getit=true;
                            while(!temp.isEmpty()){
                                sb.append(temp.remove().out());
                            }
                            sb.append("\n");
                        }
                        int[]zsArr=new int[zs.size()];
                        for(int k=0;k<zsArr.length;k++){
                           zsArr[k]=zs.remove();
                        }
                        for(int k=0;k<zsArr.length;k++){
                            for(int l=k+1;l<zsArr.length;l++){
                                checked[zsArr[k]][zsArr[l]]=true;
                            }
                        }
                    }
                  
                }
                if(!getit){
                    sb.append("No lines were found\n");
                }
            }
        System.out.print(sb);
    }
  
    static boolean coolinear(Point a,Point b,Point c){
        return ((a.getY())-(b.getY()))*((a.getX())-(c.getX()))==
               ((a.getY())-(c.getY()))*((a.getX())-(b.getX()));
    }
}
class Point implements Comparable<Point> {
    private int x;
    private int y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

   
    @Override
    public int compareTo(Point o) {
        if(this.x>o.getX()){
            return 1;
        }else if(this.x<o.getX()){
            return -1;
        }
        if(this.y>o.getY()){
            return 1;
        }else{
            return -1;
        }
    }
   
    public String out(){
        String ans=String.format("(%4d,%4d)", this.x,this.y);
        return ans;
    }
}

Thursday, 10 January 2013

UVA - 378 - Intersecting Lines


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb=new StringBuffer();
        int cases=Integer.parseInt(br.readLine());
        sb.append("INTERSECTING LINES OUTPUT\n");
        for(int i=0;i<cases;i++){
            StringTokenizer st=new StringTokenizer(br.readLine());
            Line x,y;
            x=new Line(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()),
                    Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
            y=new Line(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()),
                    Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
            if(x.getSlope()==y.getSlope()){
                if(x.getX1()==x.getX2()){
                    if(x.getX1()==y.getX1()){
                       sb.append("LINE\n");
                    }else{
                       sb.append("NONE\n");
                    }
                }
                else if(x.func(x.getX1())==y.func(x.getX1())){
                    sb.append("LINE\n");
                }else{
                    sb.append("NONE\n");
                }
            }else{
                double tempX=(double)(y.getC()-x.getC())/(x.getSlope()-y.getSlope());
                double tempY=x.func(tempX);
                String s=String.format("POINT %.2f %.2f\n", tempX,tempY);
                sb.append(s);
            }
        }
        sb.append("END OF OUTPUT\n");
        System.out.print(sb);
    }
}
class Line{
    double x1;
    double y1;
    double x2;
    double y2;
    double slope;
    double c;

    public Line(double x1, double y1, double x2, double y2) {
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
        if(x2-x1!=0)
            slope=(double)(this.y2-this.y1)/(this.x2-this.x1);
        else{
            slope=Integer.MAX_VALUE;
        }
        c=this.y2-this.x2*this.slope;
    }

    public double getX1() {
        return x1;
    }

    public void setX1(double x1) {
        this.x1 = x1;
    }

    public double getX2() {
        return x2;
    }

    public void setX2(double x2) {
        this.x2 = x2;
    }

    public double getY1() {
        return y1;
    }

    public void setY1(double y1) {
        this.y1 = y1;
    }

    public double getY2() {
        return y2;
    }

    public void setY2(double y2) {
        this.y2 = y2;
    }

    public double getC() {
        return c;
    }

    public void setC(double c) {
        this.c = c;
    }

    public double getSlope() {
        return slope;
    }

    public void setSlope(double slope) {
        this.slope = slope;
    }
  
    public double func(double x){
         return (x*slope+c);
    }
}

Friday, 28 December 2012

UVA - 438 - The Circumference of the Circle

#include <stdio.h>
#include <math.h>
#define pi 3.141592653589793

double circ(double x,double y,double z){
        double s=(x+y+z)/2;
        return (x*y*z)*pi/(2.0*sqrt(s*(s-x)*(s-y)*(s-z)));
}

double ecuD(double x1,double y1,double x2,double y2){
    return sqrt(((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1)));
}

int main() {
    double x1,y1,x2,y2,x3,y3,x,y,z;
    while (scanf("%lf %lf %lf %lf %lf %lf",&x1,&y1,&x2,&y2,&x3,&y3)==6){
        x=ecuD(x1,y1,x2,y2);
        y=ecuD(x1,y1,x3,y3);
        z=ecuD(x2,y2,x3,y3);
        printf("%.2lf\n",circ(x,y,z));
    }
    return 0;
}

UVA - 10195 - The Knights Of The Round Table

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
import javax.crypto.Mac;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb = new StringBuffer("");
        String m="";
        while((m=br.readLine())!=null){
            StringTokenizer st=new StringTokenizer(m);
            double x=Double.parseDouble(st.nextToken());
            double y=Double.parseDouble(st.nextToken());
            double z=Double.parseDouble(st.nextToken());
            double ans=rad(x, y, z);
            String f=String.format("The radius of the round table is: %.3f\n",ans);
            sb.append(f);
        }
        System.out.print(sb);
    }
   
    static double rad(double x,double y,double z){
        if(x==0 ||y==0 ||z==0)
            return 0;
        double halfP=(x+y+z)/2;
        return Math.sqrt((halfP-x)*(halfP-y)*(halfP-z)/halfP);
    }
}

Thursday, 27 December 2012

UVA - 10209 - Is This Integration ? (C solution)

#include <stdio.h>
#include <math.h>

int main()
{
    double x,y,z,l,areaOfRect;
    double pi=acos(-1);
    while(scanf("%lf",&x)==1){
        areaOfRect=x*x;
        y = areaOfRect * (1 - sqrt(3) + pi / 3);
        z = areaOfRect * (2 * sqrt(3) - 4 + pi / 3);
        l = areaOfRect * (4 - sqrt(3) - 2 * pi/ 3);
        printf("%.3f %.3f %.3f\n", y,z,l);
    }
    return 0;
}

UVA - 10209 - Is This Integration ? (Java Solution)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb=new StringBuffer("");
        String m="";
        while((m=br.readLine())!=null){
            double a=Double.parseDouble(m);
            double areaOfRect=a*a;
            double x = areaOfRect * (1 - Math.sqrt(3) + Math.PI / 3); 
            double y = areaOfRect * (2 * Math.sqrt(3) - 4 + Math.PI / 3); 
            double z = areaOfRect * (4 - Math.sqrt(3) - 2 * Math.PI / 3);
            String temp=String.format("%.3f %.3f %.3f\n", x,y,z);
            sb.append(temp);
        }
        System.out.print(sb);
    }

}

Sunday, 23 December 2012

UVA - 191 - Intersection

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb=new StringBuffer("");
        int cases=Integer.parseInt(br.readLine());
        for(int i=0;i<cases;i++){
            StringTokenizer st = new StringTokenizer(br.readLine());
            double x1=Double.parseDouble(st.nextToken());
            double y1=Double.parseDouble(st.nextToken());
            double x2=Double.parseDouble(st.nextToken());
            double y2=Double.parseDouble(st.nextToken());
            Line line=new Line(x1, y1, x2, y2);
            x1=Double.parseDouble(st.nextToken());
            y1=Double.parseDouble(st.nextToken());
            x2=Double.parseDouble(st.nextToken());
            y2=Double.parseDouble(st.nextToken());
            Rectangle rect=new Rectangle(x1, y1, x2, y2);
            if(intersecRecLine(rect, line)){
                sb.append("T\n");
            }else{
                sb.append("F\n");
            }
        }
        System.out.print(sb);
    }
   
    static boolean intersecRecLine(Rectangle x,Line y){
        if(y.getSlope()==Integer.MAX_VALUE){
           if(y.getX1()<=x.getX2() && y.getX1()>=x.getX1()){
               for(double i=Math.min(y.getY1(),y.getY2());i<=Math.max(y.getY1(),y.getY2());i++){
                   if(i<=x.getY2() && i>=x.getY1()){
                       return true;
                   }
                }
               return false;
            }else{
               return false;
           }
        }else{
            for(double i=Math.min(y.getX1(),y.getX2()),j=0;i<=Math.max(y.getX1(),y.getX2());i++){
                if(i<=x.getX2() && i>=x.getX1()){
                    j=y.func(i);
                    if(j<=x.getY2() && j>=x.getY1()){
                        return true;
                    }
                }
            }
            return false;
        }
    }
  
}class Rectangle{
    double x1;
    double y1;
    double x2;
    double y2;

    public Rectangle(double x1, double y1, double x2, double y2) {
        this.x1 = Math.min(x1, x2);
        this.y1 = Math.min(y1, y2);
        this.x2 = Math.max(x1, x2);
        this.y2 = Math.max(y1, y2);
    }
   
    public double getX1() {
        return x1;
    }

    public void setX1(double x1) {
        this.x1 = x1;
    }

    public double getX2() {
        return x2;
    }

    public void setX2(double x2) {
        this.x2 = x2;
    }

    public double getY1() {
        return y1;
    }

    public void setY1(double y1) {
        this.y1 = y1;
    }

    public double getY2() {
        return y2;
    }

    public void setY2(double y2) {
        this.y2 = y2;
    }
   
    public double getWidth(){
        return x2-x1;
    }
   
    public double getHeight(){
        return y2-y1;
    }
   
    public double getArea(){
        return getWidth()*getHeight();
    }
   
    public double getPeri(){
        return 2*(getWidth()+getHeight());
    }
}
class Line{
    double x1;
    double y1;
    double x2;
    double y2;
    double slope;
    double c;

    public Line(double x1, double y1, double x2, double y2) {
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
        if(x2-x1!=0)
            slope=(double)(this.y2-this.y1)/(this.x2-this.x1);
        else{
            slope=Integer.MAX_VALUE;
        }
        c=this.y2-this.x2*this.slope;
    }

    public double getX1() {
        return x1;
    }

    public void setX1(double x1) {
        this.x1 = x1;
    }

    public double getX2() {
        return x2;
    }

    public void setX2(double x2) {
        this.x2 = x2;
    }

    public double getY1() {
        return y1;
    }

    public void setY1(double y1) {
        this.y1 = y1;
    }

    public double getY2() {
        return y2;
    }

    public void setY2(double y2) {
        this.y2 = y2;
    }

    public double getC() {
        return c;
    }

    public void setC(double c) {
        this.c = c;
    }

    public double getSlope() {
        return slope;
    }

    public void setSlope(double slope) {
        this.slope = slope;
    }
   
    public double func(double x){
         return (x*slope+c);
    }
}

Thursday, 20 December 2012

UVA - 11909 - Soya Milk (C solution)

#include <stdio.h>
#include <math.h>

int main()
{
    int l,w,h,theta;
    double pi=acos(-1);
    while(scanf("%d %d %d %d",&l,&w,&h,&theta)==4){
        double d=l*tan(theta*pi/180.0);
        double ans;
        if(d > h){
            ans=0.5*h*h*l*w/d;
        }else{
            ans=l*w*((h)-(d*0.5));
        }
        printf("%.3lf mL\n", ans);
    }
    return 0;
}

UVA - 11909 - Soya Milk (Java Solution)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String m="";
        while((m=br.readLine())!=null){
            StringTokenizer st = new StringTokenizer(m);
            int l=Integer.parseInt(st.nextToken());
            int w=Integer.parseInt(st.nextToken());
            int h=Integer.parseInt(st.nextToken());
            int theta=Integer.parseInt(st.nextToken());
            double d=l*Math.tan(theta*Math.PI/180.0);
            double ans=l*w*((h)-(d*0.5));
            if(d > h){
                ans=0.5*h*h*l*w/d;
            }
            System.out.printf("%.3f mL\n", ans);
        }
    }
}

UVA - 10991 - Region (C Solution)

#include <stdio.h>
#include <math.h>

int main()
{
    double r1,r2,r3;
    int cases,i;
    scanf("%d",&cases);
    for(i=0;i<cases;i++){
      scanf("%lf %lf %lf",&r1,&r2,&r3);

      double m1= r2+r3;
      double m2= r1+r3;
      double m3= r1+r2;

      double theta1=acos((m2*m2+m3*m3-m1*m1)/(2*m2*m3));
      double theta2=acos((m1*m1+m3*m3-m2*m2)/(2*m1*m3));
      double theta3=acos((m2*m2+m1*m1-m3*m3)/(2*m2*m1));

      double med=(m1+m2+m3)/2.0;
      double areaOfTri=sqrt(med*(med-m1)*(med-m2)*(med-m3));

      double areaofArc1=0.5*theta1*r1*r1;
      double areaofArc2=0.5*theta2*r2*r2;
      double areaofArc3=0.5*theta3*r3*r3;

      double ans=areaOfTri-areaofArc1-areaofArc2-areaofArc3;
      printf("%.6f\n", ans);
    }
    return 0;
}

UVA - 10991 - Region (Java Solution)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int cases=Integer.parseInt(br.readLine());
        for(int i=0;i<cases;i++){
            StringTokenizer st = new StringTokenizer(br.readLine());
            double r1 = Double.parseDouble(st.nextToken());
            double r2 = Double.parseDouble(st.nextToken());
            double r3 = Double.parseDouble(st.nextToken());
           
            double m1= r2+r3;
            double m2= r1+r3;
            double m3= r1+r2;
           
            double theta1=Math.acos((m2*m2+m3*m3-m1*m1)/(2*m2*m3));
            double theta2=Math.acos((m1*m1+m3*m3-m2*m2)/(2*m1*m3));
            double theta3=Math.acos((m2*m2+m1*m1-m3*m3)/(2*m2*m1));
           
            double med=(m1+m2+m3)/2.0;
            double areaOfTri=Math.sqrt(med*(med-m1)*(med-m2)*(med-m3));
           
            double areaofArc1=0.5*theta1*r1*r1;
            double areaofArc2=0.5*theta2*r2*r2;
            double areaofArc3=0.5*theta3*r3*r3;
           
            double ans=areaOfTri-areaofArc1-areaofArc2-areaofArc3;
            System.out.printf("%.6f\n", ans);
        }
    }
}