209 - Triangular Vertices

All about problems in Volume 2. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

mute
New poster
Posts: 8
Joined: Thu Apr 13, 2006 2:16 am
Location: Dhaka
Contact:

Post by mute »

Can any one tell me What will be the Output OF this input
1 2 3 4 5 6
Triangle na invalid
Check this out ThirdEye Creative.com
Image
mute
New poster
Posts: 8
Joined: Thu Apr 13, 2006 2:16 am
Location: Dhaka
Contact:

209-Triangular Vertices

Post by mute »

I am confused :roll:
What will be the output If the Input is

Code: Select all

1 2 3 4 5 6
Triangle or Invalid

what about
/ /
\ \

\ \
/ /

this 2 hexagonal shape??
Check this out ThirdEye Creative.com
Image
mute
New poster
Posts: 8
Joined: Thu Apr 13, 2006 2:16 am
Location: Dhaka
Contact:

Post by mute »

I just got accepted..
Input is not that tricky..
Check this out ThirdEye Creative.com
Image
killerwife
New poster
Posts: 11
Joined: Tue Jan 28, 2014 2:16 am

209 - Triangular vertices help

Post by killerwife »

Code: Select all

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

int plotEdge(int vertexA, int vertexB)
{
    int firstA,lastA;
    int firstB,lastB;
    int i,k;
    int length=-1;
    firstA=lastA=firstB=lastB=1;
    for(i=1;firstA>vertexA||lastA<vertexA;i++)
    {
        firstA=firstA+i;
        lastA=lastA+i+1;
    }
    for(k=1;firstB>vertexB||lastB<vertexB;k++)
    {
        firstB=firstB+k;
        lastB=lastB+k+1;
    }
    if(firstA==firstB)
    {
        length=abs(vertexA-vertexB);
    }
    else if(firstA-firstB==vertexA-vertexB||lastA-lastB==vertexA-vertexB)
    {
        length=abs(i-k);
    }
    return length;
}

int main()
{
    char temp=0;
    temp=fgetc(stdin);
    while(temp!=EOF&&temp!='\n')
    {
        int vertices[6];
        int i=0;
        int k=0;
        int size;
        int check=0;
        int length=0;
        int validcounter=0;
        int hexagondiagonal=0;
        ungetc(temp,stdin);
        do
        {
            scanf("%d",&vertices[i]);
            i++;
        }
        while((temp=fgetc(stdin))!='\n');
        size=i;
        for(i=0;i<size;i++)
        {
            for(k=i+1;k<size;k++)
            {
                 int result=plotEdge(vertices[i],vertices[k]);
                 if(result>0)
                 {
                     if(length==0)
                     {
                         length=result;
                     }
                     else if(length!=result&&size!=6)
                     {
                         check=-1;
                         break;
                     }
                     else if(result==2*length&&size==6)
                     {
                         hexagondiagonal++;
                     }
                     validcounter++;
                 }
            }
        }
        if(size==6)
        {
            if((validcounter==11&&hexagondiagonal!=2)||(validcounter!=11&&hexagondiagonal==2))
            {
                check=-1;
            }
            else if((validcounter!=9&&hexagondiagonal==3)||(validcounter==9&&hexagondiagonal!=3))
            {
                    check=-1;
            }
        }
        if(check==-1||size==1||size==2||size==5||(size==4&&validcounter!=5)||(size==3&&validcounter!=3))
        {
            for(i=0;i<size;i++)
            {
                printf("%d ",vertices[i]);
            }
            printf("are not the vertices of an acceptable figure\n");
        }
        else
        {
            for(i=0;i<size;i++)
            {
                printf("%d ",vertices[i]);
            }
            if(size==3)
            {
                printf("are the vertices of a triangle\n");
            }
            else if(size==4)
            {
                printf("are the vertices of a parallelogram\n");
            }
            else
            {
                printf("are the vertices of a hexagon\n");
            }
        }
        if(temp=='\n')
        {
            temp=fgetc(stdin);
        }
    }
    return 0;
}
I have tested many cases, and i cant seem to find the critical ones. I dont know if 1 1 1 should be a triangle or not. I have tried multiple submits, the problem might be elsewhere, I dont know. All the test cases ive found work.
killerwife
New poster
Posts: 11
Joined: Tue Jan 28, 2014 2:16 am

Re: 209 - Triangular vertices help

Post by killerwife »

Code: Select all

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

int cmpfunc (const void * a, const void * b)
{
   return ( *(int*)a - *(int*)b );
}

int plotEdge(int vertexA, int vertexB)
{
    int firstA,lastA;
    int firstB,lastB;
    int i,k;
    int length=-1;
    firstA=lastA=firstB=lastB=1;
    for(i=1;firstA>vertexA||lastA<vertexA;i++)
    {
        firstA=firstA+i;
        lastA=lastA+i+1;
    }
    for(k=1;firstB>vertexB||lastB<vertexB;k++)
    {
        firstB=firstB+k;
        lastB=lastB+k+1;
    }
    if(firstA==firstB)
    {
        length=abs(vertexA-vertexB);
    }
    else if(firstA-firstB==vertexA-vertexB||lastA-lastB==vertexA-vertexB)
    {
        length=abs(i-k);
    }
    return length;
}

int main()
{
    char temp=0;
    temp=fgetc(stdin);
    while(temp!=EOF)
    {
        int vertices[6];
        int i=0;
        int k=0;
        int size;
        int check=0;
        int length=0;
        int validcounter=0;
        int hexagondiagonal=0;
        ungetc(temp,stdin);
        do
        {
            scanf("%d",&vertices[i]);
            i++;
        }
        while((temp=fgetc(stdin))!='\n'&&temp!=EOF);
        size=i;
        for(i=0;i<size;i++)
            {
                printf("%d ",vertices[i]);
            }
        qsort(vertices,size,sizeof(int),cmpfunc);
        for(i=0;i<size;i++)
        {
            for(k=i+1;k<size;k++)
            {
                 int result=plotEdge(vertices[i],vertices[k]);
                 if(result>=0)
                 {
                     if(length==0)
                     {
                         length=result;
                     }
                     else if(length!=result&&size!=6)
                     {
                         check=-1;
                         break;
                     }
                     else if(size==6&&length!=result)
                     {
                         hexagondiagonal++;
                     }
                     if(result==length)
                     {validcounter++;}
                 }
            }
        }
        if(size==6)
        {
            if(validcounter-hexagondiagonal<6&&hexagondiagonal>3)
            {
                check=-1;
            }
        }
        if(check==-1||size==1||size==2||size==5||(size==4&&validcounter!=5)||(size==3&&validcounter!=3))
        {
            printf("are not the vertices of an acceptable figure\n");
        }
        else
        {
            if(size==3)
            {
                printf("are the vertices of a triangle\n");
            }
            else if(size==4)
            {
                printf("are the vertices of a parallelogram\n");
            }
            else
            {
                printf("are the vertices of a hexagon\n");
            }
        }
        if(temp=='\n')
        {
            temp=fgetc(stdin);
        }
    }
    return 0;
}
debugged a lot of stuff, still wa
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 209 - Triangular vertices help

Post by brianfry713 »

The judge's input is valid and does not contain any integers < 1 or > 32767.
If there are any duplicate points then print are not the vertices of an acceptable figure.
In my AC code: 2 5 3 6 8 9 are not the vertices of an acceptable figure
Check input and AC output for thousands of problems on uDebug!
killerwife
New poster
Posts: 11
Joined: Tue Jan 28, 2014 2:16 am

Re: 209 - Triangular vertices help

Post by killerwife »

Got an accepted, thank you. Managed to get it already yesterday. There was some sort of problem with this one 1 2 2 3 3 4. After i solved it, reverted 1 1 1 to not acceptable, i got AC.
Post Reply

Return to “Volume 2 (200-299)”