精華區beta C_and_CPP 關於我們 聯絡資訊
第587 ------------------------------------------------------------ #include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #define max 1000 int main() { int i,j,k,m,n,L,num[max],F,q,w=0,M; char all[max],s[max][5],ch,*p,temp[max],re[max][4]; double x,y,r,u,d; u=(double)sqrt((double)2); while(gets(all)!=NULL) { w++; for(p=strtok(all,","),i=0;p!=NULL;p=strtok(NULL,","),i++) strcpy(s[i],p); if(i==1&&s[0][0]=='E'&&s[0][1]=='N'&&s[0][2]=='D') break; for(j=0;j<i;j++) { L=strlen(s[j]);m=0; for(k=0;k<L;k++) if(s[j][k]-'0'<10) {temp[m]=s[j][k];m++;} temp[m]='\0';num[j]=atoi(temp); for(k=m,q=0;k<L;k++,q++) re[j][q]=s[j][k]; re[j][q]='\0'; } M=i;x=0.0;y=0.0; for(i=0;i<M;i++) if(re[i][0]=='E'&&re[i][1]=='\0') x=x+num[i]; else if(re[i][0]=='W'&&re[i][1]=='\0') x=x-num[i]; else if(re[i][0]=='N'&&re[i][1]=='\0') y=y+num[i]; else if(re[i][0]=='S'&&re[i][1]=='\0') y=y-num[i]; else if(re[i][0]=='N'&&re[i][1]=='E') {r=num[i]/u;x=x+r;y=y+r;} else if(re[i][0]=='N'&&re[i][1]=='W') {r=num[i]/u;x=x-r;y=y+r;} else if(re[i][0]=='S'&&re[i][1]=='E') {r=num[i]/u;x=x+r;y=y-r;} else if(re[i][0]=='S'&&re[i][1]=='W') {r=num[i]/u;x=x-r;y=y-r;} if(w!=1) printf("\n"); printf("Map #%d\n",w); d=(double)sqrt( (double)x*x+y*y );if(d<0) d=(double)(-1)*d; printf("The treasure is located at (%.3lf,%.3lf).\n",x,y); printf("The distance to the treasure is %.3lf.\n",d); } return 0; } ------------------------------------------------------------ 一直吃WA....... -- 不斷的飛行是痛苦的事~~~~~ (>"<) -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 218.175.108.160
waterworld0:有可能死在gets喔@@ 乍看下猜的啦@@a 218.184.140.157 01/21
sailplane:可是他要切吧..一次全讀再切啊?? 218.175.108.160 01/21
waterworld0:因為gets有Buffer大小的限制@@ 218.184.140.157 01/21
aecho:可能跟gets沒關係喔~~ 220.136.38.166 01/22
aecho:他題目說一行不會超過200 char 220.136.38.166 01/22
waterworld0:剛剛寫了一下 也WA了Or2 218.184.140.157 01/22
pjacky:strtok(all,",") 改 strtok(all,"'.") 140.112.30.22 01/22
pjacky:strtok(all,",") 改 strtok(all,",.") 140.112.30.22 01/22
sailplane:改了.........還是卦.... 218.175.107.66 01/22
aecho:喔 對了 他不用一次全讀再切.... 218.166.94.168 01/22
aecho:拿到一筆就輸出一筆 這樣就行了 218.166.94.168 01/22
sailplane:這樣有影響??? 218.175.102.208 01/23
aecho:@@ 應該沒吧 ...... 218.166.90.120 01/23
> -------------------------------------------------------------------------- < 作者: aecho (星空下的鮪魚) 看板: C_and_CPP 標題: Re: [問題] ACM........... 時間: Sat Jan 22 07:49:30 2005 ※ 引述《sailplane ()》之銘言: 解出來了 @@" 我也拿了WA 後來....你測一下這筆測資 10NW,10NE,10SW,10SE. if you use windows + VC++ you may get 0.000 but with linux + gcc you get -0.000 which causes WA. 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) 上面是我找到的....改一改問題就解決了 -- 有多少笑語 就有多少哭泣 幸福的背後 總是隱藏著悲劇 -----摘自 趙寧 --找一個字代替-- -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 218.166.86.38 ※ 編輯: aecho 來自: 61.230.232.137 (01/22 19:36) > -------------------------------------------------------------------------- < 作者: aecho (星空下的鮪魚) 看板: C_and_CPP 標題: Re: [問題] ACM........... 時間: Sat Jan 22 07:59:12 2005 my code....算是交流吧.... -- #include <stdio.h> #include <string.h> #include <math.h> #include <ctype.h> #include <stdlib.h> #define BufSize 1024 #define CofSin45 0.707106781 int main(void){ char strInput[BufSize]; char strNum[BufSize]; char *ptr; int iCase = 0; int lenPtr; double xPos,yPos; double dis; double dis_xy; while (fgets(strInput,sizeof(strInput),stdin) != NULL){ /*End condition */ strtok(strInput,"\n"); if (strcmp(strInput,"END") == 0){ return 0; } /*initial */ iCase++; xPos = yPos = 0.000000001; /*if (iCase != 1){ printf("\n"); }*/ for(ptr= strtok(strInput,",."); ptr != NULL ; ptr=strtok(NULL,",.")){ /*Take care '.' */ if (strlen(ptr) < 2){ continue; } lenPtr = strlen(ptr); if ( isdigit(ptr[lenPtr-2]) ) /*case N E S W */ { strncpy(strNum,ptr,lenPtr); strNum[lenPtr-1] = '\0'; dis = (double)atoi(strNum); if (!strcmp(&ptr[lenPtr-1],"N") ){ yPos += dis; } else if (!strcmp(&ptr[lenPtr-1],"E") ) { xPos += dis; } else if (!strcmp(&ptr[lenPtr-1],"S") ) { yPos -= dis; } else if (!strcmp(&ptr[lenPtr-1],"W")) { xPos -= dis; } } else { /*case NE NW SE SW */ strncpy(strNum,ptr,lenPtr); strNum[lenPtr-2] = '\0'; dis = (double)atoi(strNum); if (!strcmp(&ptr[lenPtr-2],"NE") ) { xPos += (dis * CofSin45); yPos += (dis * CofSin45); } else if (!strcmp(&ptr[lenPtr-2],"NW") ) { xPos -= (dis * CofSin45); yPos += (dis * CofSin45); } else if (!strcmp(&ptr[lenPtr-2],"SE") ) { xPos += (dis * CofSin45); yPos -= (dis * CofSin45); } else if (!strcmp(&ptr[lenPtr-2],"SW") ) { xPos -= (dis * CofSin45); yPos -= (dis * CofSin45); } } } dis_xy = sqrt((xPos*xPos+yPos*yPos)); /* print out */ printf("Map #%d\n",iCase); printf("The treasure is located at (%.3f,%.3f).\n",xPos,yPos); printf("The distance to the treasure is %.3f.\n",dis_xy); printf("\n"); } return 0; } -- 靜待風帆將起之日 乘風隨夢.... 到那不知名的未知裡.. 探尋....深夜靜謐的想望... 探尋....潛藏心底的自己... -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 218.166.86.38
sailplane:多謝幫忙~~不過你說的那組測資 回到原點啊 218.175.98.179 01/22
sailplane:沒錯吧 我可以跑 218.175.98.179 01/22
aecho:就像英文所說的 因為compiler不同 218.166.94.168 01/22
aecho:所以可能會拿到 -0.000 218.166.94.168 01/22
aecho:我是這邊改一改就拿到accepted了 218.166.94.168 01/22
waterworld0:感謝 我也AC了Or2 這個bug沒人說 218.184.140.157 01/23
waterworld0:大概抓到死都抓不到吧@@ 218.184.140.157 01/23
sailplane:要怎改啊.if(d=(double)-0) d=(double)0;? 218.175.102.208 01/23
sailplane:麻煩教一下吧........... 218.175.102.208 01/23
sailplane:x=0.0000000001;y=0.0000000001; 218.175.102.208 01/23
sailplane:還是WA............. 218.175.102.208 01/23
> -------------------------------------------------------------------------- < 作者: aecho (星空下的鮪魚) 看板: C_and_CPP 標題: Re: [問題] ACM........... 時間: Sun Jan 23 12:09:14 2005 如果還是wa的話 我想你基本的code本身就有問題了吧 因為我和另外一位主要是卡在那一個沒人說也不會知道的bug 這個bug解決後我們就都ac了 在你的code下面有人推文說了一些東西 你有針對像這樣的data嗎 10N. 10NE. 之類的 也就是結尾是.的 另外 output的格式對嗎? acm的答案是用程式比對的 所以 不容小瑕疵 -- 有多少笑語 就有多少哭泣 幸福的背後 總是隱藏著悲劇 -----摘自 趙寧 --找一個字代替-- -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 218.166.90.120
sailplane:多謝指教~~~~^^ 218.175.107.217 01/23
> -------------------------------------------------------------------------- < 作者: waterworld0 (幹 電磁學炸掉) 看板: C_and_CPP 標題: Re: [問題] ACM........... 時間: Sun Jan 23 19:02:19 2005 PO我的code給你參考看看吧^^ 慢慢trace一下 可能會知道自己大概哪裡漏掉了 #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> #include <math.h> #define SIZE 1000 typedef struct _position{ double x; double y; }position; int main() { char buff[SIZE]; char *steps[SIZE]; position p; int times = 1; const char *delim = ","; int step_count; int i, j; char dir[5]; char digits[5]; int num; double tmpnum; int tmp1, tmp2; while(1){ scanf("%s", buff); step_count = 0; if(strcmp(buff, "END") == 0) break; else{ /*initial the position of each data*/ p.x = 0.000000001, p.y = 0.000000001; steps[step_count] = strtok(buff,delim); while (steps[step_count] != NULL){ step_count++; steps[step_count]=strtok(NULL,delim); } steps[step_count-1][strlen(steps[step_count-1])-1] = '\0'; for(i = 0; i < step_count; i++){ tmp1 = tmp2 = 0; for(j = 0; j < strlen(steps[i]); j++){ if(isalpha(steps[i][j])) dir[tmp1++] = steps[i][j]; else if(isdigit(steps[i][j])) digits[tmp2++] = steps[i][j]; } dir[tmp1] = '\0'; digits[tmp2] = '\0'; num = atoi(digits); tmpnum = (double)num; /*printf("%s %lf\n", dir, tmpnum);*/ if( strcmp(dir, "N") == 0 ){ p.y = p.y + tmpnum; }else if( strcmp(dir, "S") == 0 ){ p.y = p.y - tmpnum; }else if( strcmp(dir, "E") == 0 ){ p.x = p.x + tmpnum; }else if( strcmp(dir, "W") == 0 ){ p.x = p.x - tmpnum; }else if( strcmp(dir, "NE") == 0 ){ p.x = p.x + tmpnum/sqrt((double)2); p.y = p.y + tmpnum/sqrt((double)2); }else if( strcmp(dir, "SE") == 0 ){ p.x = p.x + tmpnum/sqrt((double)2); p.y = p.y - tmpnum/sqrt((double)2); }else if( strcmp(dir, "NW") == 0 ){ p.x = p.x - tmpnum/sqrt((double)2); p.y = p.y + tmpnum/sqrt((double)2); }else if( strcmp(dir, "SW") == 0 ){ p.x = p.x - tmpnum/sqrt((double)2); p.y = p.y - tmpnum/sqrt((double)2); } } printf("Map #%d\n", times); printf("The treasure is located at (%.3lf,%.3lf).\n", p.x, p.y); printf("The distance to the treasure is %.3lf.\n\n", sqrt(p.x*p.x+p.y*p.y)); times++; } } return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 218.184.140.157
sailplane:感謝++ 218.175.101.204 01/23