Sunday, January 9, 2011

Point in Triangle

The Problem

You are given the coordinates of the vertices of a triangle and also the coordinates of a point. You have to determine whether the point lies in the triangle or the it lies outside the triangle.

Logic Used

Distance of the point from a vertex is compared with the distances of the vertex from the other two vertices. If the distance of the point is found to be greater than any of the distances from other vertices, then the point lies outside the triangle. This is repeated for all the three vertices of the triangle.


#include<iostream>
#include<cmath>
using namespace std;
int main()
{
float dist(float,float,float,float);//Function to find the distance between two points.
float x1,x2,x3,y1,y2,y3,px,py,distance,length1,length2;
int out=0;
while(cin>>x1)
{
cin>>y1;
cin>>x2>>y2;
cin>>x3>>y3;
cin>>px>>py;
out=0;
length1=dist(x1,y1,x2,y2);
length2=dist(x1,y1,x3,y3);
distance=dist(x1,y1,px,py);
if(distance>length1 || distance>length2)
{
out=1;
}
length1=dist(x2,y2,x1,y1);
length2=dist(x2,y2,x3,y3);
distance=dist(x2,y2,px,py);
if(distance>length1 || distance>length2)
{
out=1;
}
length1=dist(x3,y3,x1,y1);
length2=dist(x3,y3,x2,y2);
distance=dist(x3,y3,px,py);
if(distance>length1 || distance>length2)
{
out=1;
}
if(out==0)
{cout<<"Inside"<<endl;}
else
{cout<<"Outside"<<endl;}
}
return 0;
}
float dist(float x1,float y1,float x2,float y2)
{
return sqrt(pow((x2-x1),2)+pow((y2-y1),2));
}

#include<iostream>


#include<cmath>


using namespace std;


int main()


{


float dist(float,float,float,float);//Function to find the distance between two points.


float x1,x2,x3,y1,y2,y3,px,py,distance,length1,length2;


int out=0;


while(cin>>x1)


{


cin>>y1;


cin>>x2>>y2;


cin>>x3>>y3;


cin>>px>>py;


out=0;


length1=dist(x1,y1,x2,y2);


length2=dist(x1,y1,x3,y3);


distance=dist(x1,y1,px,py);


if(distance>length1 || distance>length2)


{


out=1;


}


length1=dist(x2,y2,x1,y1);


length2=dist(x2,y2,x3,y3);


distance=dist(x2,y2,px,py);


if(distance>length1 || distance>length2)


{


out=1;


}


length1=dist(x3,y3,x1,y1);


length2=dist(x3,y3,x2,y2);


distance=dist(x3,y3,px,py);


if(distance>length1 || distance>length2)


{


out=1;


}


if(out==0)


{cout<<"Inside"<<endl;}


else


{cout<<"Outside"<<endl;}


}


return 0;


}


float dist(float x1,float y1,float x2,float y2)


{


return sqrt(pow((x2-x1),2)+pow((y2-y1),2));


}



Thursday, December 23, 2010

Hello world!

Welcome to blogger.com. This is your first post. Edit or delete it and start blogging!

Sunday, October 17, 2010

Pixel Enclosures





In the digital world geometric shapes are drawn using
pixels. A pixel is a square area of unit dimension. For this problem we say 2
pixels are connected if they share an edge or a vertex between them. This is
also called 8-connectivity. Your task is to find the maximum possible area of a
closed loop made up of A pixels (these are boundary pixels of the closed loop).
Area of a closed loop is the number of pixels which are completely inside the
loop or on the loop. Consider the example below:
For A = 4, you can make a close loop as follows -


This has an area of 5 with 4 pixels on the loop and 1 pixel completely inside
the loop.



Input:



First line contains an integer N,
the number of test cases.
Next N lines contain an integer A for that test case.
N <= 100
A <= 1000



Output:



Print N
lines with a single integer stating the maximum area of a closed loop.

Sample Input:
2
4
5

Sample Output:
5
6


Time Limit: 2 seconds
Memory Limit: 32 MB



#include<iostream>



using namespace std;



int main()



{



    int fun(int);



    int N,ans,A;



   
cin>>N;



    while(N>0)



    {



       
cin>>A;



       
ans=fun(A);



       
cout<<ans<<endl;



        N--;



    }



    return 0;



}



int fun(int A)



{



    if(A==0)



    {



        return 0;



    }



    else if(A==1)



    {



        return 1;



    }



    else if(A==2)



    {



        return 2;



    }



    else if(A==3)



    {



        return 3;



    }



    else if(A==4)



    {



        return 5;



    }



    else



    {



        return A + fun(A-4);



    }



}

Friday, October 15, 2010

Code for Chatruka's Parallelogram



#include<iostream>



#include<string>



using namespace std;



int main()



{



    int i,j,k,l,count=0,mov=0,n=1,end=0;



    char A[200],B[100][100],C[100][100],T[10];



    gets(T);



   
count=0;mov=0;n=1;end=0;



    if(T[0]=='E')



    {



       
gets(A);



        while((2*n*n)<strlen(A))



        {



           
n++;



        }



        if(n%2==0)



        {



           
n++;



        }



       
k=0;l=1;end=0;



        for(i=0;i<n;i++)



        {



            for(j=0;j<n;j++)



            {



               
if(k<strlen(A))



                {



                   
B[i][j]=A[k];



                   
k=k+2;



               
}



               
else



               
{



                   
end=1;



                   
break;



               
}



            }



            if(end==1)



            {break;}



        }



        while(i<n)



        {



            while(j<n)



            {



               
B[i][j]='/';



               
j++;



            }



           
i++;



           
j=0;



        }



       
end=0;



        for(i=0;i<n;i++)



        {



            for(j=0;j<n;j++)



            {



               
if(l<strlen(A))



               
{



                   
C[i][j]=A[l];



                   
l=l+2;



               
}



               
else



               
{



                   
end=1;



                   
break;



               
}



            }



            if(end==1)



            {break;}



        }



        while(i<n)



        {



            while(j<n)



            {



               
C[i][j]='/';



               
j++;



            }



           
i++;



           
j=0;



        }



       
cout<<"OUTPUT IS"<<endl;



       
cout<<A<<endl;



       
i=n/2;j=n/2;mov=0;



       
cout<<B[i][j]<<C[i][j];



       
count++;



        while(count<n*n)



        {



           
mov++;



            for(k=0;k<mov;k++)



            {



               
if(count<n*n)



               
{



                   
j--;



                   
cout<<B[i][j]<<C[i][j];



                   
count++;



               
}



            }



            for(k=0;k<mov;k++)



            {



               
if(count<n*n)



               
{



                    i--;



                   
cout<<B[i][j]<<C[i][j];



                   
count++;



               
}



            }



           
mov++;



            for(k=0;k<mov;k++)



            {



               
if(count<n*n)



               
{



                
   j++;



                   
cout<<B[i][j]<<C[i][j];



                   
count++;



               
}



            }



            for(k=0;k<mov;k++)



            {



               
if(count<n*n)



               
{



                   
i++;



                   
cout<<B[i][j]<<C[i][j];



                   
count++;



               
}



            }



        }



       
cout<<endl;



    }



    else if(T[0]=='D')



    {



       
gets(A);



        while((2*n*n)<strlen(A))



        {



           
n++;



        }



        if(n%2==0)



        {



           
n++;



        }



       
i=n/2;j=n/2;mov=0;l=0;



       
B[i][j]=A[l];l++;



       
C[i][j]=A[1];l++;



        while(count<n*n)



        {



           
mov++;



            for(k=0;k<mov;k++)



            {



               
if(count<n*n)



               
{



                   
j--;



                   
B[i][j]=A[l];l++;



                   
C[i][j]=A[l];l++;



                   
count++;



               
}



            }



            for(k=0;k<mov;k++)



            {



               
if(count<n*n)



               
{



                   
i--;



                   
B[i][j]=A[l];l++;



                   
C[i][j]=A[l];l++;



                   
count++;



               
}



            }



           
mov++;



            for(k=0;k<mov;k++)



            {



               
if(count<n*n)



               
{



                   
j++;



                   
B[i][j]=A[l];l++;



                   
C[i][j]=A[l];l++;



                   
count++;



               
}



            }



            for(k=0;k<mov;k++)



            {



               
if(count<n*n)



               
{



                   
i++;



                   
B[i][j]=A[l];l++;



                   
C[i][j]=A[l];l++;



                   
count++;



               
}



            }



        }



       
cout<<"OUTPUT IS"<<endl;



        cout<<A<<endl;



        for(i=0;i<n;i++)



        {



            for(j=0;j<n;j++)



            {



               
if(B[i][j]!='/')



               
{cout<<B[i][j];}



               
if(C[i][j]!='/')



               
{cout<<C[i][j];}



            }



        }



       
cout<<endl;



    }    



    return 0;



}

Problem for Chatruka's Parallelogram

Raja Chatruka needs a message to be delivered to his spy. The message is supposed to be encoded so that none other than the spy can decode the message, who knows the process of encoding.


Earlier the Raja and the Spy both decided to adopt the process of “expanding parallelogram code”. The method encodes the message by placing the characters in an odd order parallelogram matrix and then reading it in a clockwise spiral from the center. Each cell of the parallelogram contains two characters; the one below is filled first as well as read first. The length of message determines the order of the matrix. The order needs to be minimum. If the number of characters are less than the number of cells in the matrix the remaining cells are filled with “/”. No message contains the actual character /.


For example: If the message is “this is a problem”



The output should be “a s this iprm/leob”


Your program must be able to encode and decode the message by this process.


INPUT The input will consist of a pair of lines. The first line will contain either of the two words “ENCODE” or “DECODE” in uppercase letters. The second line will contain the message(less than 100 characters) to be encoded or decoded depending upon the first line.


OUTPUT The output will consist of three lines. The first line will print “OUTPUT IS” in uppercase letters. The second line will print the original message, and the third line the resultant message. The character „/‟ should be removed to get the decoded message.


Note: Order of a matrix refers to the number of rows or columns.


SAMPLE INPUT:


ENCODE


this is a problem


SAMPLE OUTPUT:


OUTPUT IS


this is a problem a s this iprm/leob

Sunday, August 8, 2010

Sequences



Aadarsh is fond of special type
of sequences which are first increasing then decreasing then increasing then
decreasing and so on or which are first decreasing then increasing then
decreasing and so on like 1,2,1,2,1,2 or 2,1,2,1,2,1 respectively. He wants to
find a longest sub-sequence(not necessarily contiguous) of such nos in a given sequence. Help him out to find
such sequence.

Input Specification


Each test case consists of two
input lines. The first line of a test case contains an integer L,
determining the length of the sequence . The second line of a test case
contains sequence of numbers. L is a positive integer. The sequence contains
integer.

Test Cases are given till end of
file

Output Specification


For each test case output the
Length of the longest sequence which follows the property as mentioned above in
the given input sequence.

Example Input


5
1 2 1 2 1
8
1 2 1 2 2 1 1 4
8
1 2 3 4 5 6 7 8
8
8 7 6 5 4 3 2 1
8
1 1 2 2 1 1 2 2

Example Output


5
6
2
2
4
#include<iostream>
using namespace std;
int main()
{
    int A[100],num;
    while(cin>>num)
    {
        for(int i=0;i<num;i++)
        {
            cin>>A[i];
        }
        int order=0,temp=0,count=0;
        for(int i=0;i<num-1;i++)
        {
            if(order==0)
            {
                if(A[i+1]>A[i])
                {
                     count+=2;
                     order=-1;
                }
                if(A[i+1]<A[i])
                {
                     count+=2;
                     order=1;
                }
                temp=A[i+1];
            }
            else if(order==1)
            {
                if(A[i+1]>temp)
                {
                    count++;
                    order=-1;
                }
                temp=A[i+1];
            }
            else if(order==-1)
            {
                if(A[i+1]<temp)
                {
                    count++;
                    order=1;
                }
                temp=A[i+1];
            }
        }
    cout<<count<<endl;
    }
}

Logical Error

Robin designed a logical circuit for adding two
numbers.
But he forgot to include the carry logic in his circuit. The
working of his faulty circuit for the case of adding two bits may be hence
defined as follows:
0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 0



You are given two
integers, and you have to return the result as calculated by Robin's addition
circuit.



Input Specification



The input consists of multiple test cases. Each case
consists of two integers on separate lines. End of file denotes the end of
input.



Output Specification



For each test case output the result on a separate line.



Example Input





1

0



0



0



21



11



Example Output





1

0



30
The logic:
When a sum of 1+1 is taken as 0, an error of (2^n) is created in the sum, where n is the place (units, tens etc.) where the error is committed. Hence first calculate the correct sum by actual addition of the given two numbers. Then compute the error by examining the simultaneous occurrence of the bit 1 in the binary representation of the numbers.

#include<iostream>
using namespace std;
int main()
{
          int num1, num2, x , y ,i=-1 , sum1=0 ,sum=0 ,j ,z=1 ;  
          while(cin>>num1)
          {
                    cin>>num2;
                    i=-1 , sum1=0 ,sum=0 ,j=0 ,z=1;
                    //Adding the numbers for getting the correct sum.
                    sum=num1+num2;
                    do
                    {
                             z=1;
                             x = num1% 2;
                             ++i;
                             num1=num1/2;
                             y= num2% 2;
                             num2=num2/2;
                             //The condition where the bits  of both the numbers are 1.
                             if (x==1 && y == 1)
                            {
                                     //Computing the error at that place by multiplying 2, "the number of place" times. 
                                     for(j=0;j<=i;j++)
                                    {
                                             z*=2;
                                    }
                                    //Computing the total overall error.
                                    sum1+=z;
                           }
                   }while(num1!=0 && num2!=0);
                   //Subtracting the error from the correct sum.  
                   sum-=sum1;
                   cout<<sum<<endl;
         }
         return 0;  
}