All about problems in Volume V. If there is a thread about your problem, please use it. If not, create one with its number in the subject.
Moderator: Board moderators
by birdegg » Tue Feb 01, 2005 8:00 pm
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;
}
-
birdegg
- New poster
-
- Posts: 6
- Joined: Wed Jan 05, 2005 10:42 pm
by jdmetz » Sun Jul 10, 2005 3:45 am
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
-
jdmetz
- New poster
-
- Posts: 25
- Joined: Fri May 27, 2005 5:23 pm
- Location: Ann Arbor, MI USA
by Andrew_PL » Fri Sep 09, 2005 12:29 pm
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;
}
-
Andrew_PL
- New poster
-
- Posts: 34
- Joined: Fri Sep 02, 2005 12:08 pm
- Location: Poland
by roni » Sat Sep 10, 2005 3:17 am
use these
x=10e-12; y=10e-12; as initialization. I think u will get ACC
roni(SUST)
-
roni
- New poster
-
- Posts: 11
- Joined: Tue Aug 09, 2005 11:57 am
- Location: SUST, BANGLADESH
-
by Andrew_PL » Sat Sep 10, 2005 8:53 am
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;
}
-
Andrew_PL
- New poster
-
- Posts: 34
- Joined: Fri Sep 02, 2005 12:08 pm
- Location: Poland
by worm3959 » Tue Nov 29, 2005 5:55 am
I guess that you must of been abducted lat night or something cos your definatly are not thinking straight.
-
worm3959
- New poster
-
- Posts: 4
- Joined: Tue Nov 29, 2005 5:49 am
-
by Andrew_PL » Tue Nov 29, 2005 5:29 pm
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

-
Andrew_PL
- New poster
-
- Posts: 34
- Joined: Fri Sep 02, 2005 12:08 pm
- Location: Poland
by mamun » Tue Nov 29, 2005 8:30 pm
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.

-
mamun
- A great helper
-
- Posts: 286
- Joined: Mon Oct 03, 2005 1:54 pm
- Location: Bangladesh
-
by a79v » Tue Nov 29, 2005 10:26 pm
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]
-
a79v
- New poster
-
- Posts: 3
- Joined: Mon Nov 28, 2005 2:38 pm
by Jan » Mon Dec 26, 2005 12:52 am
I think you are not allowed use '#include<iomanip>'. Erase it and test...
-
Jan
- Guru
-
- Posts: 1334
- Joined: Wed Jun 22, 2005 10:58 pm
- Location: Dhaka, Bangladesh
-
by Emilio » Tue Jan 03, 2006 7:21 am
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.
-
Emilio
- Experienced poster
-
- Posts: 163
- Joined: Sun Oct 17, 2004 8:31 pm
- Location: Murcia, Spain
by darkos32 » Thu Nov 23, 2006 3:51 am
hi,i got WA at problem 587
this is my code :
- Code: Select all
#include <stdio.h>
#include <string.h>
#include <math.h>
double x,y;
void utara(int pindah){
y+=pindah;
}
void selatan(int pindah){
y-=pindah;
}
void timur(int pindah){
x+=pindah;
}
void barat(int pindah){
x-=pindah;
}
void utarabarat(int pindah){
double temp = sqrt((pindah*pindah)/2);
x-=temp;
y+=temp;
}
void utaratimur(int pindah){
double temp = sqrt((pindah*pindah)/2);
x+=temp;
y+=temp;
}
void selatanbarat(int pindah){
double temp = sqrt((pindah*pindah)/2);
x-=temp;
y-=temp;
}
void selatantimur(int pindah){
double temp = sqrt((pindah*pindah)/2);
x+=temp;
y-=temp;
}
void main(){
freopen("treasure.in","r",stdin);
freopen("treasure.out","w",stdout);
char s[205] = "";
int awal = 1;
while(scanf("%s",&s)==1){
if(strcmp(s,"END")==0) break;
int i = 0;
int byk = strlen(s);
int mypindah = 0;
int faktor = 1;
x = 0;y = 0;
while(i<byk){
if(((int) s[i])>=48 && ((int) s[i])<=57){
mypindah = (mypindah * faktor) + (((int) s[i]) - 48);
faktor =10;
i++;
}
else {
faktor = 1;
if(s[i]=='.'){
i+=byk;
continue;
}
else if(s[i]=='N' && (s[i+1]==',' || s[i+1]=='.')) {
utara(mypindah);
mypindah = 0;
i+=2;
if(s[i+1]=='.') i+=byk;
continue;
}
else if(s[i]=='W' && (s[i+1]==',' || s[i+1]=='.')) {
barat(mypindah);
mypindah = 0;
if(s[i+1]=='.') i+=byk;
i+=2;continue;
}
else if(s[i]=='E' && (s[i+1]==',' || s[i+1]=='.')) {
timur(mypindah);
mypindah = 0;
if(s[i+1]=='.') i+=byk;
i+=2;continue;
}
else if(s[i]=='S' && (s[i+1]==',' || s[i+1]=='.')) {
selatan(mypindah);
mypindah = 0;
if(s[i+1]=='.') i+=byk;
i+=2;continue;
}
else if(s[i]=='N' && s[i+1]=='W') utarabarat(mypindah);
else if(s[i]=='N' && s[i+1]=='E') utaratimur(mypindah);
else if(s[i]=='S' && s[i+1]=='W') selatanbarat(mypindah);
else if(s[i]=='S' && s[i+1]=='E') selatantimur(mypindah);
if(s[i+2]=='.') i+=byk;
mypindah = 0;
i+=3;
}
}
printf("Map #%d\n",awal);
printf("The treasure is located at (%.3f,%.3f).\n",x,y);
double jarak = sqrt((x*x) + (y*y));
printf("The distance to the treasure is %.3f.\n\n",jarak);
awal++;
}
}
can anyone help me please ?
thanks...
-
darkos32
- New poster
-
- Posts: 27
- Joined: Tue Jul 25, 2006 8:10 am
- Location: Indonesia
-
by ishtiaq ahmed » Mon Apr 21, 2008 11:06 pm
My code seems to be okay by the given input. But its WA by judge. Anybody help me? Here is my code
- Code: Select all
The code is deleted after AC.
Last edited by
ishtiaq ahmed on Wed Apr 23, 2008 11:19 am, edited 1 time in total.
No venture no gain
with best regards
------------------------
ishtiaq ahmed
-
ishtiaq ahmed
- Learning poster
-
- Posts: 53
- Joined: Sat Jul 29, 2006 7:33 am
- Location: (CSE,DU), Dhaka,Bangladesh
by mak(cse_DU) » Tue Apr 22, 2008 8:58 pm
if(casno > 1)
puts("");
Problem Statement say:
Print a blank line after each test case
Not between two test case.
Mak
Help me PLZ!!
-
mak(cse_DU)
- Learning poster
-
- Posts: 72
- Joined: Tue May 30, 2006 5:57 pm
- Location: bangladesh
by ishtiaq ahmed » Wed Apr 23, 2008 11:18 am
Thanks a lot mak
No venture no gain
with best regards
------------------------
ishtiaq ahmed
-
ishtiaq ahmed
- Learning poster
-
- Posts: 53
- Joined: Sat Jul 29, 2006 7:33 am
- Location: (CSE,DU), Dhaka,Bangladesh
Return to Volume V
Who is online
Users browsing this forum: No registered users and 1 guest