Page 2 of 3
587 There's treasure everywhere! -got WA
Posted: Tue Feb 01, 2005 8:00 pm
by birdegg
can somebody give me some suggestions... any input sample is very helpful.
i debug this for many hours.
thanks first.
Code: Select all
#include<stdio.h>
#include<string.h>
#include<math.h>
#define MAX 50000
int main(void){
int n,lens,map=0;
char str[MAX],s[10];
char *tokenPtr;
long double x,y;
while(gets(str)){
if(strcmp(str,"END")==0)
break;
map++;
x=10e-12 ; y=10e-12 ;
tokenPtr = strtok(str,",");
while(tokenPtr!=NULL){
sscanf(tokenPtr,"%d %s",&n,s);
lens=strlen(s);
if(((strcmp(s,"N")==0&&(lens==1)) || (strcmp(s,"N.")==0&&(lens==2))))
{y=y+(double)n;}
if(((strcmp(s,"W")==0&&(lens==1)) || (strcmp(s,"W.")==0&&(lens==2))))
{x=x-(double)n;}
if(((strcmp(s,"S")==0&&(lens==1)) || (strcmp(s,"S.")==0&&(lens==2))))
{y=y-(double)n;}
if(((strcmp(s,"E")==0&&(lens==1)) || (strcmp(s,"E.")==0&&(lens==2))))
{x=x+(double)n;}
if(((strcmp(s,"NW")==0&&(lens==2)) || (strcmp(s,"NW.")==0&&(lens==3))))
{x=x-(double)n/sqrt(2); y=y+(double)n/sqrt(2);}
if(((strcmp(s,"SW")==0&&(lens==2)) || (strcmp(s,"SW.")==0&&(lens==3))))
{x=x-(double)n/sqrt(2); y=y-(double)n/sqrt(2);}
if(((strcmp(s,"SE")==0&&(lens==2)) || (strcmp(s,"SE.")==0&&(lens==3))))
{/x=x+(double)n/sqrt(2); y=y-(double)n/sqrt(2);}
if(((strcmp(s,"NE")==0&&(lens==2)) || (strcmp(s,"NE.")==0&&(lens==3))))
{x=x+(double)n/sqrt(2); y=y+(double)n/sqrt(2);}
tokenPtr=strtok(NULL,",");
}
printf("Map #%d\n",map);
printf("The treasure is located at (%.3Lf,%.3Lf).\n",x,y);
printf("The distance to the treasure is %.3Lf.\n",sqrt(x*x+y*y));
printf("\n");
}
return 0;
}
sqrt and long double
Posted: Sun Jul 10, 2005 3:45 am
by jdmetz
sqrt() returns a double, not a long double, so you are printing a garbage distance. Use %.3lf for the distance printing.
Also, your SE comparison has a syntax error o the line following it:
(/x=x
should be just
(x=x
587 There's treasure everywhere! WA
Posted: Fri Sep 09, 2005 12:29 pm
by Andrew_PL
Hi all
I've tried couple of times, but I'm constantly getting WA! I don't know what could be wrong with such a simple code. It works with samples.
Thx for any help!
Code: Select all
#include <stdio.h>
#include <math.h>
int main()
{
char line[201], *p;
double x, y, d;
for (int m=1;;m++)
{
scanf("%s", line);
if (line[0] == 'E') break;
x = y = 0;
int steps=0, dir=-1;
for (p=line;;p++)
{
char znak = *p;
if (znak == '.') break;
if (znak == ',') continue;
if (znak >= '0' && znak <= '9') {
steps *= 10;
steps += znak - '0';
continue;
}
if (znak >= 'A' && znak <= 'Z') {
p++;
if (znak == 'N' && (*p == ',' || *p == '.')) dir = 0;
if (znak == 'N' && *p == 'E') dir = 1;
if (znak == 'E') dir = 2;
if (znak == 'S' && *p == 'E') dir = 3;
if (znak == 'S' && (*p == ',' || *p == '.')) dir = 4;
if (znak == 'S' && *p == 'W') dir = 5;
if (znak == 'W' && (*p == ',' || *p == '.')) dir = 6;
if (znak == 'N' && *p == 'W') dir = 7;
}
if (dir != -1) {
if (dir == 0) // N
y += steps;
else if (dir == 1) { // NE
x += 0.707107*steps;
y += 0.707107*steps;
}
else if (dir == 2) // E
x += steps;
else if (dir == 3) { // SE
x += 0.707107*steps;
y -= 0.707107*steps;
}
else if (dir == 4) // S
y -= steps;
else if (dir == 5) { // SW
x -= 0.707107*steps;
y -= 0.707107*steps;
}
else if (dir == 6) // W
x -= steps;
else if (dir == 7) { // NW
x -= 0.707107*steps;
y += 0.707107*steps;
}
dir = -1;
steps = 0;
}
}
d = sqrt(x*x+y*y);
x += 0.0000001; // epsilon
y += 0.0000001;
d += 0.0000001;
printf("Map #%d\n", m);
printf("The treasure is located at (%.3f,%.3f).\n",x,y);
printf("The distance to the treasure is %.3f.\n\n", d);
}
return 0;
}
treasure
Posted: Sat Sep 10, 2005 3:17 am
by roni
use these
x=10e-12; y=10e-12; as initialization. I think u will get ACC
Posted: Sat Sep 10, 2005 8:53 am
by Andrew_PL
I had, but I've got WA again. Mayby error is not in rounding, but in data processing? Here's my new code:
Code: Select all
int main()
{
char line[250], *p;
double x, y, d;
const double p22 = sqrt(2.0)/2.0;
const double epsilon = 1E-7;
for (int m=1;;m++)
{
gets(line);
if (line[0]=='E') break;
x = y = 0.0;
int steps=0, dir=-1;
for (p=line;;p++)
{
char znak = *p;
if (znak == '.') break;
if (znak == ',') continue;
if (znak >= '0' && znak <= '9')
{
steps *= 10;
steps += znak - '0';
continue;
}
if (znak >= 'A' && znak <= 'Z')
{
p++;
if (znak == 'N' && (*p == ',' || *p == '.')) dir = 0;
if (znak == 'N' && *p == 'E') dir = 1;
if (znak == 'E') dir = 2;
if (znak == 'S' && *p == 'E') dir = 3;
if (znak == 'S' && (*p == ',' || *p == '.')) dir = 4;
if (znak == 'S' && *p == 'W') dir = 5;
if (znak == 'W' && (*p == ',' || *p == '.')) dir = 6;
if (znak == 'N' && *p == 'W') dir = 7;
}
if (dir != -1)
{
if (dir == 0) // N
y += (double)steps;
else if (dir == 1) // NE
{
x += p22*(double)steps;
y += p22*(double)steps;
}
else if (dir == 2) // E
x += (double)steps;
else if (dir == 3) // SE
{
x += p22*(double)steps;
y -= p22*(double)steps;
}
else if (dir == 4) // S
y -= (double)steps;
else if (dir == 5) // SW
{
x -= p22*(double)steps;
y -= p22*(double)steps;
}
else if (dir == 6) // W
x -= (double)steps;
else if (dir == 7) // NW
{
x -= p22*(double)steps;
y += p22*(double)steps;
}
dir = -1;
steps = 0;
}
} // for (int m=1;...
d = sqrt(x*x+y*y);
d += epsilon; //dodanie epsilon'u aby uniknąć "-0"
x += epsilon;
y += epsilon;
printf("Map #%d\n", m);
printf("The treasure is located at (%.3f,%.3f).\n",x,y);
printf("The distance to the treasure is %.3f.\n\n", d);
}
return 0;
}
Abducted
Posted: Tue Nov 29, 2005 5:55 am
by worm3959
I guess that you must of been abducted lat night or something cos your definatly are not thinking straight.
Re: Abducted
Posted: Tue Nov 29, 2005 5:29 pm
by Andrew_PL
worm3959 wrote:I guess that you must of been abducted lat night or something cos your definatly are not thinking straight.
So whare's the mistake Mr. Wise Guy

Posted: Tue Nov 29, 2005 8:30 pm
by mamun
Your problem is in parsing the input. In
Code: Select all
if (znak >= 'A' && znak <= 'Z')
{
p++;
...
}
Don't increment p. Rather check the latter things by *(p+1). This should solve it.

587 - There's treasure everywhere!
Posted: Tue Nov 29, 2005 10:17 pm
by a79v
i have done 587 and it runs very well in my vc++ 6.0 compiler and bloodshed compiler,but when i submitted it i got compiler error,can anyone help me check.
#include<iostream>
#include<cctype>
#include<string>
#include<cmath>
#include<iomanip>
using namespace std;
void move(string,float&,float&,float);
float pt(int);
int main()
{
float s;
string command,input;
int number,counter,temp,length;
float x,y,dis;
number=1;
s=sqrt(2)/2;
while(cin>>input)
{
if(input=="END")
break;
else
{
x=0;
y=0;
length=input.length();
temp=0;
for(counter=0;counter<length;counter++)
{
if((input[counter]==',')||(input[counter]=='.'))
{
command=input.substr(temp,counter-temp);
move(command,x,y,s);
temp=counter+1;
}
}
dis=sqrt(x*x+y*y);
cout<<"Map #"<<number<<endl;
cout<<showpoint<<fixed<<"The treasure is located at "<<setprecision(3)
<<'('<<x<<','<<y<<')'<<'.'<<endl;
cout<<"The distance to the treasure is "<<dis<<'.'<<endl;
number++;
}
}
return 0;
}
void move(string command,float& x,float& y,float s)
{
int length,counter;
float rd,rds;
string dis,dir;
length=command.length();
for(counter=0;counter<length;counter++)
{
if(isdigit(command[counter])==0)
break;
}
dis=command.substr(0,counter-0);
dir=command.erase(0,counter-0);
length=dis.length();
rd=0;
for(counter=0;counter<length;counter++)
{
rd=(dis[counter]-'0')*pt(length-counter-1)+rd;
}
rds=rd*s;
if(dir=="N")
y=y+rd;
else if(dir=="S")
y=y-rd;
else if(dir=="W")
x=x-rd;
else if(dir=="E")
x=x+rd;
else if(dir=="NE")
{
x=x+rds;
y=y+rds;
}
else if(dir=="NW")
{
x=x-rds;
y=y+rds;
}
else if(dir=="SW")
{
x=x-rds;
y=y-rds;
}
else
{
x=x+rds;
y=y-rds;
}
}
float pt(int power)
{
int counter;
float sum=1;
for(counter=0;counter<power;counter++)
{
sum=sum*10;
}
return sum;
}
587 there's treasure everywhere compiler error
Posted: Tue Nov 29, 2005 10:26 pm
by a79v
i can compile my code in vc++,but here i got compiler error,i thought it is becuse the different standard. can anyone help me?thx
Code: Select all
#include<iostream>
#include<cctype>
#include<string>
#include<cmath>
#include<iomanip>
using namespace std;
void move(string,float&,float&,float);
float pt(int);
int main()
{
float s;
string command,input;
int number,counter,temp,length;
float x,y,dis;
number=1;
s=sqrt(2)/2;
while(1)
{
cin>>input;
if(input=="END")
break;
else
{
x=0.0;
y=0.0;
length=input.length();
temp=0;
for(counter=0;counter<length;counter++)
{
if((input[counter]==',')||(input[counter]=='.'))
{
command=input.substr(temp,counter-temp);
move(command,x,y,s);
temp=counter+1;
}
}
dis=sqrt(x*x+y*y);
cout<<"Map #"<<number<<endl;
cout<<showpoint<<fixed<<"The treasure is located at "<<setprecision(3)
<<'('<<x<<','<<y<<')'<<'.'<<endl;
cout<<"The distance to the treasure is "<<dis<<'.'<<endl;
number++;
}
}
return 0;
}
void move(string command,float& x,float& y,float s)
{
int length,counter;
float rd,rds;
string dis,dir;
length=command.length();
for(counter=0;counter<length;counter++)
{
if(isdigit(command[counter])==0)
break;
}
dis=command.substr(0,counter-0);
dir=command.erase(0,counter-0);
length=dis.length();
rd=0;
for(counter=0;counter<length;counter++)
{
rd=(dis[counter]-'0')*pt(length-counter-1)+rd;
}
rds=rd*s;
if(dir=="N")
y=y+rd;
else if(dir=="S")
y=y-rd;
else if(dir=="W")
x=x-rd;
else if(dir=="E")
x=x+rd;
else if(dir=="NE")
{
x=x+rds;
y=y+rds;
}
else if(dir=="NW")
{
x=x-rds;
y=y+rds;
}
else if(dir=="SW")
{
x=x-rds;
y=y-rds;
}
else
{
x=x+rds;
y=y-rds;
}
}
float pt(int power)
{
int counter;
float sum=1.0;
for(counter=0;counter<power;counter++)
{
sum=sum*10.0;
}
return sum;
}
[quote]
[/quote]
Posted: Wed Nov 30, 2005 5:09 pm
by helloneo
check this out..
=>
s = sqrt(2.0)/2
Posted: Thu Dec 15, 2005 4:32 am
by a79v
haha,still got comiler error dunno why leh
Posted: Mon Dec 19, 2005 3:05 pm
by chunyi81
What helloneo pointed out was not the error. The error lies here:
Code: Select all
cout<<showpoint<<fixed<<"The treasure is located at "<<setprecision(3)<<'('<<x<<','<<y<<')'<<'.'<<endl;
The UVA g++ compiler does not recognize showpoint and fixed flags this way. Change that to:
Code: Select all
cout.showpoint;
cout.fixed;
cout<<"The treasure is located at "<<setprecision(3)
<<'('<<x<<','<<y<<')'<<'.'<<endl;
Posted: Mon Dec 26, 2005 12:52 am
by Jan
I think you are not allowed use '#include<iomanip>'. Erase it and test...
Posted: Tue Jan 03, 2006 7:21 am
by Emilio
The fault is another, iomanip library is allowed.
This line give the CE
Code: Select all
cout<<showpoint<<fixed<<"The treasure is located at "<<setprecision(3)
<<'('<<x<<','<<y<<')'<<'.'<<endl;
By other hand I think that this post would must be in C++ Forum because the trouble is the language.