Can any one tell me What will be the Output OF this input
1 2 3 4 5 6
Triangle na invalid
209 - Triangular Vertices
Moderator: Board moderators
209-Triangular Vertices
I am confused
What will be the output If the Input is
Triangle or Invalid
what about
/ /
\ \
\ \
/ /
this 2 hexagonal shape??

What will be the output If the Input is
Code: Select all
1 2 3 4 5 6
what about
/ /
\ \
\ \
/ /
this 2 hexagonal shape??
-
- New poster
- Posts: 11
- Joined: Tue Jan 28, 2014 2:16 am
209 - Triangular vertices help
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;
}
-
- New poster
- Posts: 11
- Joined: Tue Jan 28, 2014 2:16 am
Re: 209 - Triangular vertices help
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;
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 209 - Triangular vertices help
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
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!
-
- New poster
- Posts: 11
- Joined: Tue Jan 28, 2014 2:16 am
Re: 209 - Triangular vertices help
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.