2009-01-18 18:15:13來源不明
ACM 587 There’s treasure everywhere!
我的right(水平)up(垂直)
當我這兩個變數都用double之時,right=-0.000 up=0.000
當我這兩個變數都用float之時,right=0.000 up=-0.000
一整個就是3條線,所以混用唄。
應該我對型態還不是很熟,暫時先這樣吧!
/*************************************************************/
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include<math.h>
- main()
- {
- char n[201],END[4]="END";
- int temp,a,b,c,time=1;
- float right;
- double up;
- while(gets(n)!=0&&strcmp(n,END)!=0)
- {
- right=0.0;up=0.0;temp=0;
- for(a=0;a<strlen(n);a++)
- {
- if(n[a]<=57&&n[a]>=48)
- temp=temp*10+n[a]-48;
- else if(n[a]=='N'||n[a]=='E'||n[a]=='W'||n[a]=='S')
- {
- switch(n[a])
- {
- case 'N':{
- if(n[a+1]=='E')
- {
- a++;
- up=up+sqrt(temp*temp/2.0);
- right=right+sqrt(temp*temp/2.0);
- }
- else if(n[a+1]=='W')
- {
- a++;
- up=up+sqrt(temp*temp/2.0);
- right=right-sqrt(temp*temp/2.0);
- }
- else
- {
- up=up+temp;
- }
- temp=0;
- break;
- }
- case 'E':{
- right=right+temp;
- temp=0;
- break;
- }
- case 'W':{
- right=right-temp;
- temp=0;
- break;
- }
- case 'S':{
- if(n[a+1]=='E')
- {
- a++;
- up=up-sqrt(temp*temp/2.0);
- right=right+sqrt(temp*temp/2.0);
- }
- else if(n[a+1]=='W')
- {
- a++;
- up=up-sqrt(temp*temp/2.0);
- right=right-sqrt(temp*temp/2.0);
- }
- else
- {
- up=up-temp;
- }
- temp=0;
- break;
- }
- }
- }
- }
- if(right-0==0) right=0;
- if(up-0==0) up=0;
- printf("Map #%d\n",time);
- printf("The treasure is located at (%.3f,%.3lf).\n",right,up);
- printf("The distance to the treasure is %.3f.\n",sqrt(right*right+up*up));
- time++;
- }
- return 0;
- }