587 - There's treasure everywhere!
Moderator: Board moderators
I think 587 is an easy problem. But I got WA. Could anyone help me? The following is my source code.
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
int main()
{
char map[300],*dir,*m;
double sq2,x,y;
int s,caseno=1;
sq2=sqrt(0.5);
while (scanf("%s",map) && strcmp(map,"END"))
{
x=y=0.0;
m=map;
while (*m)
{ s=0;
while (isdigit(*m)) s=10*s+(*m++ - '0');
for (dir=m;isalpha(*m);m++);
if (*m==',') *m++=0; else *m=0;
if (strcmp(dir,"N")==0) y+=s;
if (strcmp(dir,"S")==0) y-=s;
if (strcmp(dir,"E")==0) x+=s;
if (strcmp(dir,"W")==0) x-=s;
if (strcmp(dir,"NE")==0) {x+=sq2*s; y+=sq2*s;}
if (strcmp(dir,"NW")==0) {x-=sq2*s; y+=sq2*s;}
if (strcmp(dir,"SE")==0) {x+=sq2*s; y-=sq2*s;}
if (strcmp(dir,"SW")==0) {x-=sq2*s; y-=sq2*s;}
}
printf("Map #%dn",caseno++);
printf("The treasure is located at (%.3f,%.3f).n",x,y);
printf("The distance to the treasure is %.3f.nn",sqrt(x*x+y*y));
}
return 0;
}
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
int main()
{
char map[300],*dir,*m;
double sq2,x,y;
int s,caseno=1;
sq2=sqrt(0.5);
while (scanf("%s",map) && strcmp(map,"END"))
{
x=y=0.0;
m=map;
while (*m)
{ s=0;
while (isdigit(*m)) s=10*s+(*m++ - '0');
for (dir=m;isalpha(*m);m++);
if (*m==',') *m++=0; else *m=0;
if (strcmp(dir,"N")==0) y+=s;
if (strcmp(dir,"S")==0) y-=s;
if (strcmp(dir,"E")==0) x+=s;
if (strcmp(dir,"W")==0) x-=s;
if (strcmp(dir,"NE")==0) {x+=sq2*s; y+=sq2*s;}
if (strcmp(dir,"NW")==0) {x-=sq2*s; y+=sq2*s;}
if (strcmp(dir,"SE")==0) {x+=sq2*s; y-=sq2*s;}
if (strcmp(dir,"SW")==0) {x-=sq2*s; y-=sq2*s;}
}
printf("Map #%dn",caseno++);
printf("The treasure is located at (%.3f,%.3f).n",x,y);
printf("The distance to the treasure is %.3f.nn",sqrt(x*x+y*y));
}
return 0;
}
-
- Experienced poster
- Posts: 169
- Joined: Wed Oct 31, 2001 2:00 am
- Location: Singapore
-
- Experienced poster
- Posts: 169
- Joined: Wed Oct 31, 2001 2:00 am
- Location: Singapore
587
The judge told me that this program ran for 0.000 seconds and gave a WA 

Code: Select all
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
int main(void)
{
char map[201];
char dir[3];
int c = 0;
int d;
int i;
float x, y, s;
s = sqrt(0.5);
dir[2] = 0;
i = 0;
while (scanf("%s", map) && strcmp(map, "END")) {
i++;
x = 0.0;
y = 0.0;
c = 0;
d = 0;
while (map[c] != '.') {
if (isdigit(map[c]))
d = (d*10) + (map[c]-'0');
if (isalpha(map[c])) {
dir[0] = map[c];
if (isalpha(map[c+1])) {
dir[1] = map[++c];
} else dir[1] = 0;
if (!strcmp(dir, "N")) y += (float)d;
if (!strcmp(dir, "S")) y -= (float)d;
if (!strcmp(dir, "E")) x += (float)d;
if (!strcmp(dir, "W")) x -= (float)d;
if (!strcmp(dir, "NE")) { x += s * (float)d; y+= s * (float)d; }
if (!strcmp(dir, "NW")) { x -= s * (float)d; y+= s * (float)d; }
if (!strcmp(dir, "SE")) { x += s * (float)d; y-= s * (float)d; }
if (!strcmp(dir, "SW")) { x -= s * (float)d; y-= s * (float)d; }
}
if (map[c] == ',') {
d = 0;
c++;
continue;
}
c++;
}
printf("Map #%i\n", i);
printf("The treasure is located at (%0.3f,%0.3f).\n", x, y);
printf("The distance to the treasure is %0.3f.\n\n", sqrt(x*x + y*y));
}
return 0;
}
HI~
I could tell you there's precision error~~
Try to make the precision error smaller and your code will be got accepted.
Try to make the precision error smaller and your code will be got accepted.
Can You tell me what is wrong? I compared output of my program with the output of my friend program for some test data generated by us.
Data are identical but his code is accepted and mine got WA
#include <stdio.h>
#include <math.h>
#include <string.h>
int main()
{
char w[205];
int map,i;
int x,y,px,py,step;
double fx,fy,fp;
fp=(sqrt(2)/2);
map=0;
while(gets(w))
{
if( strcmp( w, "END" ) == 0 ) break;
map++;
i=0;
x=y=0;
px=py=0;
while(w[i]!='.')
{
step=0;
while((w[i]>='0') && (w[i]<='9'))
{
step*=10;
step+=(w[i]-'0');
i++;
}
switch(w[i++])
{
case 'W':
x-=step;
break;
case 'N':
if(w[i]=='W')
{
px-=step;
py+=step;
i++;
}else
if(w[i]=='E')
{
px+=step;
py+=step;
i++;
}else y+=step;
break;
case 'E':
x+=step;
break;
case 'S':
if(w[i]=='W')
{
px-=step;
py-=step;
i++;
}else
if(w[i]=='E')
{
px+=step;
py-=step;
i++;
}else y-=step;
break;
}
}
fx=x+px*fp;
fy=y+py*fp;
printf("Map #%d\n",map);
printf("The treasure is located at (%0.3f,%0.3f).\n",fx,fy);
printf("The distance to the treasure is %0.3f.\n\n",sqrt(fx*fx+fy*fy));
}
return 0;
}
please help!!!
Data are identical but his code is accepted and mine got WA

#include <stdio.h>
#include <math.h>
#include <string.h>
int main()
{
char w[205];
int map,i;
int x,y,px,py,step;
double fx,fy,fp;
fp=(sqrt(2)/2);
map=0;
while(gets(w))
{
if( strcmp( w, "END" ) == 0 ) break;
map++;
i=0;
x=y=0;
px=py=0;
while(w[i]!='.')
{
step=0;
while((w[i]>='0') && (w[i]<='9'))
{
step*=10;
step+=(w[i]-'0');
i++;
}
switch(w[i++])
{
case 'W':
x-=step;
break;
case 'N':
if(w[i]=='W')
{
px-=step;
py+=step;
i++;
}else
if(w[i]=='E')
{
px+=step;
py+=step;
i++;
}else y+=step;
break;
case 'E':
x+=step;
break;
case 'S':
if(w[i]=='W')
{
px-=step;
py-=step;
i++;
}else
if(w[i]=='E')
{
px+=step;
py-=step;
i++;
}else y-=step;
break;
}
}
fx=x+px*fp;
fy=y+py*fp;
printf("Map #%d\n",map);
printf("The treasure is located at (%0.3f,%0.3f).\n",fx,fy);
printf("The distance to the treasure is %0.3f.\n\n",sqrt(fx*fx+fy*fy));
}
return 0;
}
please help!!!
587 treasure every where
change %0.3f to %.3f, I accepted because of this.
ali.
ali.
i also got WA
here is my code :
can u help me!!!
here is my code :
Code: Select all
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
main() {
int i,j,n,count=1;
float dx,xx,yy,d;
char *x,step[999],dir[999],buff[999],buf[999];
dx=sqrt(2)/2;
while(1) {
gets(buf);
if(strcmp(buf,"END")==0) return 0;
x = buf;
xx = 0, yy=0;
while(1) {
sscanf(x,"%[^.,\n]s",buff);
x+=(strlen(buff));
sscanf(buff,"%[0123456789]s",step);
sscanf(buff+strlen(step),"%s",dir);
if(strlen(dir)==2) d = dx;
else d = 1;
for(i=0;i<atoi(step);i++) {
for(j=0;j<dir[j];j++) {
switch(dir[j]) {
case 'S' : yy+=-d; break;
case 'N' : yy+=d; break;
case 'W' : xx+=-d; break;
case 'E' : xx+=d; break;
}
}
}
if(x[0]=='.') break;
else x++;
}
printf("Map #%d\n",count++);
printf("The treasure is located at (%.3f,%.3f).\n",xx,yy);
printf("The distance to the treasure is %.3f.\n",sqrt(xx*xx+yy*yy));
}
}
peace...
587 WA
Can somebody tell me why the following code is wrong:
#include<iostream>
#include<vector>
#include<string>
#include<cmath>
using namespace std;
#define PI 3.14159265
#define ANGLE (PI*45/180)
int main()
{
int steps, count = 1;
char d1, d2;
char temp;
cin >> temp;
while(temp != 'E')
{
vector< pair< double, pair < char, char > > > list;
pair< double, pair< char, char > > t;
cin.putback(temp);
cin >> steps;
cin >> d1 >> d2;
bool addLast = false;
while(d2 != '.')
{
if(d2 != ',' && d2 != '.')
{
t.first = steps;
t.second.first = d1;
t.second.second = d2;
cin >> d2;
}
else
{
t.first = steps;
t.second.first = d1;
t.second.second = 'Z';
}
list.push_back(t);
if(d2 != '.')
{
cin >> steps >> d1 >> d2;
if(d2 == '.')
addLast = true;
}
}
if(addLast)
{
t.first = steps;
t.second.first = d1;
t.second.second = 'Z';
list.push_back(t);
}
/*
for(int i = 0 ; i < list.size(); i++)
{
cerr << list.first << list.second.first
<< list.second.second << ",";
cerr << endl;
*/
double X = 10e-12, Y = 10e-12, angle;
for(int i = 0; i < list.size(); i++)
{
// cerr << list.first << list.second.first
// << list.second.second << endl;
if(list.second.second == 'Z')
{
if(list.second.first == 'N')
Y += list.first;
else if(list.second.first == 'S')
Y -= list[i].first;
else if(list[i].second.first == 'E')
X += list[i].first;
else if(list[i].second.first == 'W')
X -= list[i].first;
}
else
{
if(list[i].second.first == 'N' && list[i].second.second == 'W')
{
X -= cos(ANGLE) * double(list[i].first);
Y += sin(ANGLE) * double(list[i].first);
// Y += tempY;
// X -= tempX;
}
else if(list[i].second.first == 'N' && list[i].second.second == 'E')
{
X += cos(ANGLE) * double(list[i].first);
Y += sin(ANGLE) * double(list[i].first);
// Y += tempY;
// X += tempX;
}
else if(list[i].second.first == 'S' && list[i].second.second == 'E')
{
X += cos(ANGLE) * double(list[i].first);
Y -= sin(ANGLE) * double(list[i].first);
// Y -= tempY;
// X += tempX;
}
else if(list[i].second.first == 'S' && list[i].second.second == 'W')
{
X -= cos(ANGLE) * double(list[i].first);
Y -= sin(ANGLE) * double(list[i].first);
// Y -= tempY;
// X -= tempX;
}
}
// cerr << X << " " << Y << endl;
}
cout.setf(ios::showpoint);
cout.setf(ios::fixed);
cout.precision(3);
double distance = sqrt(double(Y * Y + X * X));
cout << "Map #" << count << endl;
cout << "The treasure is located at (" << X << "," << Y << ")." << endl;
cout << "The distance to the treasure is " << distance <<"." << endl << endl;
count++;
cin >> temp;
}
return 1;
}[cpp][/cpp]
#include<iostream>
#include<vector>
#include<string>
#include<cmath>
using namespace std;
#define PI 3.14159265
#define ANGLE (PI*45/180)
int main()
{
int steps, count = 1;
char d1, d2;
char temp;
cin >> temp;
while(temp != 'E')
{
vector< pair< double, pair < char, char > > > list;
pair< double, pair< char, char > > t;
cin.putback(temp);
cin >> steps;
cin >> d1 >> d2;
bool addLast = false;
while(d2 != '.')
{
if(d2 != ',' && d2 != '.')
{
t.first = steps;
t.second.first = d1;
t.second.second = d2;
cin >> d2;
}
else
{
t.first = steps;
t.second.first = d1;
t.second.second = 'Z';
}
list.push_back(t);
if(d2 != '.')
{
cin >> steps >> d1 >> d2;
if(d2 == '.')
addLast = true;
}
}
if(addLast)
{
t.first = steps;
t.second.first = d1;
t.second.second = 'Z';
list.push_back(t);
}
/*
for(int i = 0 ; i < list.size(); i++)
{
cerr << list.first << list.second.first
<< list.second.second << ",";
cerr << endl;
*/
double X = 10e-12, Y = 10e-12, angle;
for(int i = 0; i < list.size(); i++)
{
// cerr << list.first << list.second.first
// << list.second.second << endl;
if(list.second.second == 'Z')
{
if(list.second.first == 'N')
Y += list.first;
else if(list.second.first == 'S')
Y -= list[i].first;
else if(list[i].second.first == 'E')
X += list[i].first;
else if(list[i].second.first == 'W')
X -= list[i].first;
}
else
{
if(list[i].second.first == 'N' && list[i].second.second == 'W')
{
X -= cos(ANGLE) * double(list[i].first);
Y += sin(ANGLE) * double(list[i].first);
// Y += tempY;
// X -= tempX;
}
else if(list[i].second.first == 'N' && list[i].second.second == 'E')
{
X += cos(ANGLE) * double(list[i].first);
Y += sin(ANGLE) * double(list[i].first);
// Y += tempY;
// X += tempX;
}
else if(list[i].second.first == 'S' && list[i].second.second == 'E')
{
X += cos(ANGLE) * double(list[i].first);
Y -= sin(ANGLE) * double(list[i].first);
// Y -= tempY;
// X += tempX;
}
else if(list[i].second.first == 'S' && list[i].second.second == 'W')
{
X -= cos(ANGLE) * double(list[i].first);
Y -= sin(ANGLE) * double(list[i].first);
// Y -= tempY;
// X -= tempX;
}
}
// cerr << X << " " << Y << endl;
}
cout.setf(ios::showpoint);
cout.setf(ios::fixed);
cout.precision(3);
double distance = sqrt(double(Y * Y + X * X));
cout << "Map #" << count << endl;
cout << "The treasure is located at (" << X << "," << Y << ")." << endl;
cout << "The distance to the treasure is " << distance <<"." << endl << endl;
count++;
cin >> temp;
}
return 1;
}[cpp][/cpp]
587 WA why?
Anyone know why this is WA?
[cpp]
#include <iostream>
#include <vector>
#include <cmath>
#include <cctype>
using namespace std;
const double PI=3.1415926535898;
int main()
{
double x, y, d, real;
char ch;
vector<char> map;
int mapNum=0;
while(true)
{
x=0; y=0; d=0;
do
{
cin.get(ch);
map.push_back(ch);
}while(ch != '.' && ch != '\n');
cin.get();
if(map[0] == 'E' && map[1] =='N' && map[2] == 'D')
exit(0);
for(int i=0; i<map.size(); i++)
{
if(isdigit(map))
{
d = (d*10) + (map - '0');
}
else if(isalpha(map) && map != ',' && map != '.')
{
switch(map)
{
case 'N':
if(map[i+1] == 'E')
{
x += (d*sin(PI/4));
y += (d*sin(PI/4));
}
else if(map[i+1] == 'W')
{
x -= (d*sin(PI/4));
y += (d*sin(PI/4));
}
else if(isdigit(map[i-1]))
y += d;
break;
case 'E':
if(isdigit(map[i-1]))
x += d;
break;
case 'S':
if(map[i+1] == 'E')
{
x += (d*sin(PI/4));
y -= (d*sin(PI/4));
}
else if(map[i+1] == 'W')
{
x -= (d*sin(PI/4));
y -= (d*sin(PI/4));
}
else if(isdigit(map[i-1]))
y -= d;
break;
case 'W':
if(isdigit(map[i-1]))
x -= d;
break;
}
d=0;
}
}
cout.setf(ios::fixed);
cout.precision(3);
real = sqrt(pow(x,2) + pow(y,2));
cout << "Map #" << ++mapNum << endl;
cout << "The treasure is located at (" << x << "," << y << ")." << endl;
cout << "The distance to the treasure is " << real << endl << endl;
map.clear();
}
return 0;
}
[/cpp]
[cpp]
#include <iostream>
#include <vector>
#include <cmath>
#include <cctype>
using namespace std;
const double PI=3.1415926535898;
int main()
{
double x, y, d, real;
char ch;
vector<char> map;
int mapNum=0;
while(true)
{
x=0; y=0; d=0;
do
{
cin.get(ch);
map.push_back(ch);
}while(ch != '.' && ch != '\n');
cin.get();
if(map[0] == 'E' && map[1] =='N' && map[2] == 'D')
exit(0);
for(int i=0; i<map.size(); i++)
{
if(isdigit(map))
{
d = (d*10) + (map - '0');
}
else if(isalpha(map) && map != ',' && map != '.')
{
switch(map)
{
case 'N':
if(map[i+1] == 'E')
{
x += (d*sin(PI/4));
y += (d*sin(PI/4));
}
else if(map[i+1] == 'W')
{
x -= (d*sin(PI/4));
y += (d*sin(PI/4));
}
else if(isdigit(map[i-1]))
y += d;
break;
case 'E':
if(isdigit(map[i-1]))
x += d;
break;
case 'S':
if(map[i+1] == 'E')
{
x += (d*sin(PI/4));
y -= (d*sin(PI/4));
}
else if(map[i+1] == 'W')
{
x -= (d*sin(PI/4));
y -= (d*sin(PI/4));
}
else if(isdigit(map[i-1]))
y -= d;
break;
case 'W':
if(isdigit(map[i-1]))
x -= d;
break;
}
d=0;
}
}
cout.setf(ios::fixed);
cout.precision(3);
real = sqrt(pow(x,2) + pow(y,2));
cout << "Map #" << ++mapNum << endl;
cout << "The treasure is located at (" << x << "," << y << ")." << endl;
cout << "The distance to the treasure is " << real << endl << endl;
map.clear();
}
return 0;
}
[/cpp]
try this input
The reason is the difference between the two compiler when processing 'double' multiplication or you can just mention it precision error.
solution:
you may initial x, y with 1e-8(or other small number)
if you use windows + VC++ you may get 0.000 but with linux + gcc you get -0.000 which causes WA.10NW,10NE,10SW,10SE.
The reason is the difference between the two compiler when processing 'double' multiplication or you can just mention it precision error.
solution:
you may initial x, y with 1e-8(or other small number)