## 209 - Triangular Vertices

Moderator: Board moderators

bobi1978
New poster
Posts: 13
Joined: Tue Jul 22, 2003 1:57 pm
Contact:
There are two types of triangles:
--
\/

/\
--

Three types of parallelograms:
---
/ /
---

---
\ \
---

/\
/ \
\ /
\/

One type of hexagon:
---
/ \
\ /
---

It seems that there are no inputs of type :
--
/ /
\ \
--
because I got ACC WITH AND WITHOUT checking for this figure to be a hexagon.

samueljj
New poster
Posts: 18
Joined: Fri Jul 18, 2003 5:24 am
I've checked all the possible formations. But don't understand why still getiing WA? Can anybody provide some critical inputs
novice programmer

Ivor
Experienced poster
Posts: 150
Joined: Wed Dec 26, 2001 2:00 am
Location: Tallinn, Estonia
10 11 15
8 11 20
6 8 17

Ivor
There is a theory which states that if ever anyone discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.

Charlla
New poster
Posts: 12
Joined: Mon Oct 13, 2003 1:33 am

### Problem 209 - Input Examples

Hy!

Can anyone help me? I need some critical input examples for problem 209.

Thanks,

Charlla

Unlimited
New poster
Posts: 1
Joined: Fri Mar 19, 2004 10:54 pm

### 209 WA

Why does this code get WA?
My program analises: 2 different triangle types, 3 different paralelogram types; 1 hexagon type;
[cpp]
#include<stdio.h>
#include<iostream>
#include<sstream>

using namespace std;

struct Coord {
int x;
int y;
};

int triangle(Coord *p, long *d);
int parallelogram(Coord *p,long *d);
int hexagon(Coord *p,long *d);

int sort(int *p,int size) {
int i,j;
int k;
for(i=0;i<size-1;i++) {
if (p>p[i+1]) {
j=i+1;
while ((j>0) && (p[j-1]>p[j])) {
//cout << "j=" <<j << " ";
k=p[j-1];
p[j-1]=p[j];
p[j]=k;
j--;
}
}
}
return 0;
}

{
int i=0;
istringstream ist(str);
while (ist >> p) {
printf("%d ",p);
if (i<=6)
i++;
}
return i;
}

int main() {
int p[8]; //points;
int i,count;
char line[5000];
Coord c[6];
long d[6];
while (!cin.eof()) {
if(count>6 || count<3 || count==5) {
printf("are not the vertices of an acceptable figure\n");
}
else {
sort(p,count);
d[0]=0;
//convert p to (x,y)
for(i=0;i<count;i++)
{
if (i>0)
d=d[i-1];
while((d+1)*(d+2)/2 < p) d++;
c.x=(d[i]+1)*(d[i]+2)/2-p[i];
c[i].y=d[i]-c[i].x;
}
switch(count) {
case 3:if (triangle(c,d))
printf("are the vertices of a triangle\n");
else printf("are not the vertices of an acceptable figure\n");
break;
case 4:if (parallelogram(c,d))
printf("are the vertices of a parallelogram\n");
else printf("are not the vertices of an acceptable figure\n");
break;
case 6:if (hexagon(c,d))
printf("are the vertices of a hexagon\n");
else printf("are not the vertices of an acceptable figure\n");
break;
}

}
}
return 0;
}

int triangle(Coord* p,long *d) {
if ((d[0]==d[1]) && (p[0].x!=p[1].x) &&
(p[0].x==p[2].x) && (p[1].y==p[2].y))
return 1;
else if ((p[1].x+p[1].y==p[2].x+p[2].y) && (p[1].x!=p[2].x) &&
(p[0].x==p[2].x) && (p[0].y==p[1].y))
return 1;
else return 0;
}
int parallelogram(Coord *p,long *d) {
if ((d[0]==d[1]) && (p[0].x!=p[1].x) &&
(d[2]==d[3]) && (p[2].x!=p[3].x) && (d[0]<d[2])) {
if ((p[0].y==p[2].y) && (p[1].y==p[3].y) && (p[0].x==p[3].x))
return 1;
else if ((p[0].x==p[2].x) && (p[1].x==p[3].x) && (p[1].y==p[2].y))
return 1;
else return 0;
}

if ((d[1]==d[2]) && (d[0]<d[1]) && (d[1]<d[3]) &&
(p[0].x==p[2].x) && (p[0].y==p[1].y) &&
(p[1].x==p[3].x) && (p[2].y==p[3].y))
return 1;
return 0;

}

int hexagon(Coord *p,long *d) {
if ((d[0]==d[1]) && (d[2]==d[3]) && (d[4]==d[5]) && (p[0].x!=p[1].x) &&
(p[0].y==p[2].y) && (p[1].y==p[4].y) && (p[3].y==p[5].y) &&
(p[0].x==p[5].x) && (p[1].x==p[3].x) && (p[2].x==p[4].x))
return 1;
else return 0;
}
[/cpp]

raiku
New poster
Posts: 2
Joined: Sun Sep 14, 2003 5:29 pm
Location: Spain

### 209 - Triangular Vertices

Hi!

I've got a question about problem 209. A polygon with side length 0 is permitted? I mean, is 1 1 1 a valid triangle?

I'm getting WA again and again and I don't know any other special case...

Thank you very much!!

Bernat

junbin
Experienced poster
Posts: 174
Joined: Mon Dec 08, 2003 10:41 am
in case you don't realise.. there are:

2 types of triangles
3 types of parralleogram
1 type of hexagon

GreenPenInc
Learning poster
Posts: 53
Joined: Sat May 01, 2004 9:31 pm
Contact:

### reading input whose end is not signalled

OK, I'm officially sick of reading in input whose end isn't signalled by a zero or something. Example: problem 209. I've had the logic down for a while now, and it runs like a dream on my own computer, but I alternate between RTE and TLE when I submit. What's a good way to handle cases where you keep on reading until EOF? I'd love to hear multiple people's thoughts on this. Thanks!
_-(GPI)-_

"Finally I have freed myself from the clutches of the garbage fairy!"

shamim
A great helper
Posts: 498
Joined: Mon Dec 30, 2002 10:10 am
Location: Bozeman, Montana, USA
If you are using C/C++
Take the entire line of input into a char array, and then use
strtok().

GreenPenInc
Learning poster
Posts: 53
Joined: Sat May 01, 2004 9:31 pm
Contact:
Hmm... thanks, that's what I have been doing. I just hate doing it

What REALLY burns me up is the following. I've been writing a function to take a string and return an int. It all compiles fine on my machine. The following also compiles on Valladolid.

[cpp]
int make_int(string s)
{
int result = 0, i, temp;
for (i = 0; i < s.length(); i++)
{
//result = result * 10;
temp = s.at(i) - 48;
//result += temp;
//result = result + (s.at(i) - '0');
}
return result;

/*
for (int i = 0; i < s.length(); i++)
{
result *= 10;
result += (int)(s.at(i) - '0');
}
return result;
*/
}
[/cpp]

[cpp]
The following does NOT.int make_int(string s)
{
int result = 0, i, temp;
for (i = 0; i < s.length(); i++)
{
//result = result * 10;
temp = s.at(i) - 48;
result += temp;
//result = result + (s.at(i) - '0');
}
return result;

/*
for (int i = 0; i < s.length(); i++)
{
result *= 10;
result += (int)(s.at(i) - '0');
}
return result;
*/
}

[/cpp]

Now, why the hell would result += temp, when they're both integers, cause me to get TLE? I have isolated it (through scores of intentionally wrong submissions) to this very line of code. What the hell is going on?
_-(GPI)-_

"Finally I have freed myself from the clutches of the garbage fairy!"

Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:
All my programs have the following shape:

[cpp]while(true){
Typename t;
if(cin >> t)break;
}[/cpp]
or
[cpp]while(true){
Typename t;
cin >> t;
if(t == EndOfInputCharacter)break;
}[/cpp]

It is also very easy to use scanf or fgets instead of cin here. In the first case, just look at the value returned by reading function (0 means EOF), in second - compare t with EndOfInputCharacter. So generally everything stays the same, and if the first input value is the number of cases, I simply ignore it.

GreenPenInc
Learning poster
Posts: 53
Joined: Sat May 01, 2004 9:31 pm
Contact:
Okay, after asking around in #c++ (and getting badgered for the bad habits I developed by doing competitions ) I found the following scheme. As always, it works great on my own comp, but I'm getting a SIGSEGV on the Online Judge. Help?!

[cpp]
int main()
{
int nums[6], length, i;
char * line;
string msg;
while (cin.getline(line, 256))
{
length = 0;
istringstream iss(line);
while (iss >> nums[length++]);
length--;
msg = "are not the vertices of an acceptable figure";
switch(length)
{
case 3:
if (check_triangle(nums))
msg = "are the vertices of a triangle";
break;
case 4:
if (check_parallelogram(nums))
msg = "are the vertices of a parallelogram";
break;
case 6:
if (check_hexagon(nums))
msg = "are the vertices of a hexagon";
break;
}
for (i = 0; i < length; i++)
cout << nums << " ";
cout << msg << endl;
}
return 0;
}
[/cpp]
_-(GPI)-_

"Finally I have freed myself from the clutches of the garbage fairy!"

GreenPenInc
Learning poster
Posts: 53
Joined: Sat May 01, 2004 9:31 pm
Contact:
FOR ANYONE WHO GOT AC

I ONLY need to see your INPUT. I have tried everything and am at the end of my rope. Please, just show me how to input using cin. Here's my main method. Works GREAT on my computer.

[cpp]

int main()
{
int nums[6], length, i;
char line[257];
string msg;
while (!cin.getline(line, 256).eof())
{
length = 0;
istringstream iss(line);
while (iss >> nums[length++]);
length--;
msg = "are not the vertices of an acceptable figure";
switch(length)
{
case 3:
if (check_triangle(nums))
msg = "are the vertices of a triangle";
break;
case 4:
if (check_parallelogram(nums))
msg = "are the vertices of a parallelogram";
break;
case 6:
if (check_hexagon(nums))
msg = "are the vertices of a hexagon";
break;
}
for (i = 0; i < length; i++)
cout << nums << " ";
cout << msg << endl;
}

return 0;
}

[/cpp]

This gives me TLE on the Judge; it doesn't know when to stop seeking input. I have over 50 submissions trying to debug this. Just help me. Somebody. Please.
_-(GPI)-_

"Finally I have freed myself from the clutches of the garbage fairy!"

Guru
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany
I am astonished that it works on your computer;

char * line;

This doesn't allocate any memory, so when you use
cin.getline(line,256);
where should the function getline write its result?

Just tried it on my computer, and it works, too. But I think that this is just luck, because line points to some address. Now if you write char *line = NULL you will see that it will not work.
So the thing you have to do is:
char line[258];

CDiMa
Experienced poster
Posts: 214
Joined: Fri Oct 17, 2003 5:49 pm
Location: Genova

char * line;

This doesn't allocate any memory, so when you use
cin.getline(line,256);
where should the function getline write its result?

Just tried it on my computer, and it works, too. But I think that this is just luck, because line points to some address. Now if you write char *line = NULL you will see that it will not work.
So the thing you have to do is:
char line[258];