587 - There's treasure everywhere!

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

Moderator: Board moderators

birdegg
New poster
Posts: 6
Joined: Wed Jan 05, 2005 10:42 pm

587 There's treasure everywhere! -got WA

Post 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;
}

jdmetz
New poster
Posts: 25
Joined: Fri May 27, 2005 5:23 pm
Location: Ann Arbor, MI USA

sqrt and long double

Post 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

Andrew_PL
New poster
Posts: 34
Joined: Fri Sep 02, 2005 12:08 pm
Location: Poland

587 There's treasure everywhere! WA

Post 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;
}

roni
New poster
Posts: 11
Joined: Tue Aug 09, 2005 11:57 am
Location: SUST, BANGLADESH
Contact:

treasure

Post by roni »

use these
x=10e-12; y=10e-12; as initialization. I think u will get ACC
roni(SUST)

Andrew_PL
New poster
Posts: 34
Joined: Fri Sep 02, 2005 12:08 pm
Location: Poland

Post 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;
}

worm3959
New poster
Posts: 4
Joined: Tue Nov 29, 2005 5:49 am
Contact:

Abducted

Post by worm3959 »

I guess that you must of been abducted lat night or something cos your definatly are not thinking straight.

Andrew_PL
New poster
Posts: 34
Joined: Fri Sep 02, 2005 12:08 pm
Location: Poland

Re: Abducted

Post 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 ;)

mamun
A great helper
Posts: 286
Joined: Mon Oct 03, 2005 1:54 pm
Location: Bangladesh
Contact:

Post 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. :wink:

a79v
New poster
Posts: 3
Joined: Mon Nov 28, 2005 2:38 pm

587 - There's treasure everywhere!

Post 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;
}

a79v
New poster
Posts: 3
Joined: Mon Nov 28, 2005 2:38 pm

587 there's treasure everywhere compiler error

Post 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]

helloneo
Guru
Posts: 516
Joined: Mon Jul 04, 2005 6:30 am
Location: Seoul, Korea

Post by helloneo »

check this out..

Code: Select all

s=sqrt(2)/2; 
=>

s = sqrt(2.0)/2

a79v
New poster
Posts: 3
Joined: Mon Nov 28, 2005 2:38 pm

Post by a79v »

haha,still got comiler error dunno why leh

chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post 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; 

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan »

I think you are not allowed use '#include<iomanip>'. Erase it and test...
Ami ekhono shopno dekhi...
HomePage

Emilio
Experienced poster
Posts: 163
Joined: Sun Oct 17, 2004 8:31 pm
Location: Murcia, Spain

Post 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.

Post Reply

Return to “Volume 5 (500-599)”