Page 1 Maybe the problem is in acuracy... but I use pi=2.0*acosl(0.0)... Maybe it's printing DANGER! but I use if (result<99.995) printf("DANGER!\n"); Any ideas? Sorry, I've found my mistake :-[ Did you find your mistake? What is this? I have WA3 too... type = extended!!! I always use extended if real type is needed... Maybe test #3 is some special case? Distance is about 100 miles. Possibly, you make checking something like "distance < 100 then print "Dangerous!!!'". Instead, you should check not equality of number (dist < 100), but convert it into string, and then check that length(s) < 6 (it equals to condition s < '100.00') Thanks for help! But my mistake was in other... I've got AC finally try const double PI=acos(-1.0); and you will get AC Sorry, can't place code here. It looks fine, but got WA#1. Who had the same bug? Plase, tell me, what can be wrong. Maybe, reading data is bad? for (i = 0; i < 9; i++) { cin.getline(s[i],300); //scanf("%[^\n]",s[i]); ch = getc(stdin); } Thank you. P.S. Quite stupid message, like "find bug,please , but I don't know where". and i too have wa 1 Edited by author 25.05.2008 20:53 I got AC on 1331 but can't got AC on 1030. I think it read data. Please write here tolik-tol@inbox.ru ThaNks. I used PI=3.14159265358 in my program and got WA5 many times. When I added a few digits to PI, I got AC! That's cause the last digit should be 9 with the occuracy you use. ;-) To avoid such situation, write in the following way: pi = 2 * acos( 0. ); 1) In the sample test my answer is 52.046... so why the correct answer is 52.04 it must be 52.05? 2) I get WA#6... does anybody had it? I don't know what's the problem... This is my source... Edited by author 25.03.2005 15:03 your program isn't accurate enough: it calculates 52.046 while mine 52.04423 so correct is 52.04. At test 6 again, yours computes 91.914 (writing 91.91) and mine 91.91681 (writing 91.92) so WA... (By the way, I used C, if it matters) Edited by author 24.03.2005 00:00 Edited by author 24.03.2005 00:00 I've rewrite program on С++. But now I have WA#3. =( Ofcourse I know C++ not so good and probably made a mistake somewhere... Edited by author 25.03.2005 15:03 My program had WA Test#7. Last two lines were: printf("The distance to the iceberg: %.2lf miles.\n",ans); if (ans<99.999) printf("DANGER!"); But when I changed only the last one: if (ans<99.995) printf("DANGER!"); I got AC! (Now I think, that I should compare with 100 miles not answer, but it with two digits: so 99.996 is 100.00, but 99.994 is 99.99.) Yes you are right! I have had same problem. It would be better with few clarifications in the problem descriptions... Edited by author 01.02.2005 17:53 Thank you for the hint. I'll never think of that! Edited by author 18.07.2006 20:12 You is good man! ver. #2 =) double round( double d ) {//окр. до двух знаков d *= 100; if( long(d*10)%10>=5 ) ++d; return (double(long(d)))/100; } ans = round( ans ); printf( "The distance to the iceberg: %.2lf miles.\n", ans ); if( ans < 100/*!!!*/ ) puts( "DANGER!" ); ver. #3 =) ans = floor( ans*100 + 0.5 )/100; printf( "The distance to the iceberg: %.2lf miles.\n", ans ); if( ans < 100 ) puts( "DANGER!" ); Edited by author 14.09.2011 22:30 I wrote the problem for the second time, but also couldn't understand that.( It needs clarification. Edited by author 21.03.2013 19:37 Some weeks ago I had AC... Now this solution gets WA, 1... Why??? There is no reason to change the tests especialy the first one. This problem is ok and tests were not changed. Here's my prog which is getting WA. According to my algo, I think the sample output should be twice the original number -- 104.09 instead of 52.04. program ural1030; const pi=3.14159265359; r=6875; var s:string; longi,lati,ax,ay,az,bx,by,bz,d,si,co,angle,arc:real; procedure recog(s:string;var angle:real); function value(p,q:byte):byte; var junk:word; begin val(copy(s,p,q-p+1),value,junk); end; begin angle:=value(1,pos('^',s)-1); delete(s,1,pos('^',s)); angle:=angle+value(1,pos('''',s)-1)/60; delete(s,1,pos('''',s)); angle:=angle+value(1,pos('"',s)-1)/3600; delete(s,1,pos('"',s)); angle:=angle*pi/180; if (s[2]='S') or (s[2]='W') then angle:=-angle; end; begin readln;readln;readln; readln(s);recog(s,lati);readln(s);delete(s,1,4);recog(s,longi); ax:=r*cos(lati)*cos(longi);ay:=r*cos(lati)*sin(longi);az:=r*sin(lati); readln; readln(s);recog(s,lati);readln(s);delete(s,1,4);recog(s,longi); bx:=r*cos(lati)*cos(longi);by:=r*cos(lati)*sin(longi);bz:=r*sin(lati); d:=sqrt(sqr(ax-bx)+sqr(ay-by)+sqr(az-bz)); si:=d/2/r; co:=sqrt(1-sqr(si)); angle:=arctan(si/co); arc:=r*angle*2; writeln('The distance to the iceberg: ',arc:0:2,' miles.'); if arc<100 then writeln('DANGER!'); end. The problem gives the diameter of the Earth (not radius!) I think that longitude can be only Western or Eastern. Am I wrong? Hey, judges, can you check the description or you are too lazy? if it's northern longitude then the point is over the Ecuator (Z axis is positive); if it's southern it's below Ecuator. There are only 90 degrees because over 90 degrees you change the latitude. kinda difficult to understand this problem, but easy formulas after... {No.1030 Titanic.} Const pi=3.1415926535897932384626433832795; Var Char1,NS1,EW1,NS2,EW2:Char; A1,B1,C1,D1,E1,F1,A2,B2,C2,D2,E2,F2:Integer; SqrD,Alpha,L,X1,Y1,Z1,X2,Y2,Z2:Real; Function ReadNum:Integer; Var Str1:String; Code,Num:Integer; Char1:Char; Begin Str1:='##'; Read(Str1[1]); Read(Str1[2]); Read(Char1); If Char1 In ['0'..'9'] Then Str1:=Str1+Char1; Val(Str1,Num,Code); ReadNum:=Num; End; Procedure Cal(Theta1,Theta2:Real;NS,EW:Char;Var X,Y,Z:Real); Begin If NS='N' Then Z:=Sin(Theta1) Else Z:=-Sin(Theta1); X:=Cos(Theta2)*Cos(Theta1); If EW='E' Then Y:=Sin(Theta2)*Cos(Theta1) Else Y:=-Sin(Theta2)*Cos(Theta1); End; Begin ReadLn; ReadLn; ReadLn; A1:=ReadNum; B1:=ReadNum; C1:=ReadNum; Read(Char1); Read(NS1); ReadLn; Read(Char1); Read(Char1); Read(Char1); Read(Char1); D1:=ReadNum; E1:=ReadNum; F1:=ReadNum; Read(Char1); Read(EW1); ReadLn; ReadLn; A2:=ReadNum; B2:=ReadNum; C2:=ReadNum; Read(Char1); Read(NS2); ReadLn; Read(Char1); Read(Char1); Read(Char1); Read(Char1); D2:=ReadNum; E2:=ReadNum; F2:=ReadNum; Read(Char1); Read(EW2); ReadLn; ReadLn; Cal((((C1/60)+B1)/60+A1)/180*pi,(((F1/60) +E1)/60+D1)/180*pi,NS1,EW1,X1,Y1,Z1); Cal((((C2/60)+B2)/60+A2)/180*pi,(((F2/60) +E2)/60+D2)/180*pi,NS2,EW2,X2,Y2,Z2); If (Abs(X1+X2)<1E-7) And (Abs(Y1+Y2)<1E-7) And (Abs(Z1+Z2)<1E-7) Then Alpha:=pi Else Begin SqrD:=Sqr(X1-X2)+Sqr(Y1-Y2)+Sqr(Z1-Z2); Alpha:=Arctan(Sqrt(SqrD)/2/Sqrt(1-SqrD/4))*2; End; L:=Alpha*6875/2; WriteLn('The distance to the iceberg: ',L:0:2,' miles.'); If 100-L>1E-7 Then WriteLn('DANGER!'); End. This possibly mean that you try to access the element of the array with the number that doesn't exist or something like that. Good Luck!!! {No.1030 Titanic.} {$N+} Const pi=3.1415926535897932384626433832795; Var Char1,NS1,EW1,NS2,EW2:Char; A1,B1,C1,D1,E1,F1,A2,B2,C2,D2,E2,F2:Integer; SqrD,Alpha,L,X1,Y1,Z1,X2,Y2,Z2:Double; Function ReadNum:Integer; Var Str1:String; Code,Num:Integer; Char1:Char; Begin Str1:=''; Read(Char1); Str1:=Str1+Char1; Read(Char1); If Char1 In ['0'..'9'] Then Begin Str1:=Str1+Char1; Read(Char1); If Char1 In ['0'..'9'] Then Str1:=Str1+Char1; End; Val(Str1,Num,Code); ReadNum:=Num; End; Procedure Cal(Theta1,Theta2:Double;NS,EW:Char;Var X,Y,Z:Double); Begin If NS='N' Then Z:=Sin(Theta1) Else Z:=-Sin(Theta1); X:=Cos(Theta2)*Cos(Theta1); If EW='E' Then Y:=Sin(Theta2)*Cos(Theta1) Else Y:=-Sin(Theta2)*Cos(Theta1); End; Begin ReadLn; ReadLn; ReadLn; A1:=ReadNum; B1:=ReadNum; C1:=ReadNum; Read(Char1); Read(NS1); ReadLn; Read(Char1); Read(Char1); Read(Char1); Read(Char1); D1:=ReadNum; E1:=ReadNum; F1:=ReadNum; Read(Char1); Read(EW1); ReadLn; ReadLn; A2:=ReadNum; B2:=ReadNum; C2:=ReadNum; Read(Char1); Read(NS2); ReadLn; Read(Char1); Read(Char1); Read(Char1); Read(Char1); D2:=ReadNum; E2:=ReadNum; F2:=ReadNum; Read(Char1); Read(EW2); ReadLn; ReadLn; Cal((((C1/60)+B1)/60+A1)/180*pi,(((F1/60) +E1)/60+D1)/180*pi,NS1,EW1,X1,Y1,Z1); Cal((((C2/60)+B2)/60+A2)/180*pi,(((F2/60) +E2)/60+D2)/180*pi,NS2,EW2,X2,Y2,Z2); If (Abs(X1+X2)<1E-10) And (Abs(Y1+Y2)<1E-10) And (Abs(Z1+Z2)<1E- 10) Then Alpha:=pi Else Begin SqrD:=Sqr(X1-X2)+Sqr(Y1-Y2)+Sqr(Z1-Z2); Alpha:=Arctan(Sqrt(SqrD)/2/Sqrt(1-SqrD/4))*2; End; L:=Alpha*6875/2; WriteLn('The distance to the iceberg: ',L:0:2,' miles.'); If 100-L>0 Then WriteLn('DANGER!'); End. {No.1030 Titanic.} {$N+} Const pi=3.1415926535897932384626433832795; Var Char1,NS1,EW1,NS2,EW2:Char; A1,B1,C1,D1,E1,F1,A2,B2,C2,D2,E2,F2:Integer; SqrD,Alpha,L,X1,Y1,Z1,X2,Y2,Z2:Double; Function ReadNum:Integer; Var Str1:String; Code,Num:Integer; Char1:Char; Begin Str1:=''; Read(Char1); Str1:=Str1+Char1; Read(Char1); If Char1 In ['0'..'9'] Then Begin Str1:=Str1+Char1; Read(Char1); If Char1 In ['0'..'9'] Then Str1:=Str1+Char1; End; Val(Str1,Num,Code); ReadNum:=Num; End; Procedure Cal(Theta1,Theta2:Double;NS,EW:Char;Var X,Y,Z:Double); Begin If NS='N' Then Z:=Sin(Theta1) Else Z:=-Sin(Theta1); X:=Cos(Theta2)*Cos(Theta1); If EW='E' Then Y:=Sin(Theta2)*Cos(Theta1) Else Y:=-Sin(Theta2)*Cos(Theta1); End; Begin ReadLn; ReadLn; ReadLn; A1:=ReadNum; B1:=ReadNum; C1:=ReadNum; Read(Char1); Read(NS1); ReadLn; Read(Char1); Read(Char1); Read(Char1); Read(Char1); D1:=ReadNum; E1:=ReadNum; F1:=ReadNum; Read(Char1); Read(EW1); ReadLn; ReadLn; A2:=ReadNum; B2:=ReadNum; C2:=ReadNum; Read(Char1); Read(NS2); ReadLn; Read(Char1); Read(Char1); Read(Char1); Read(Char1); D2:=ReadNum; E2:=ReadNum; F2:=ReadNum; Read(Char1); Read(EW2); ReadLn; ReadLn; Cal((((C1/60)+B1)/60+A1)/180*pi,(((F1/60) +E1)/60+D1)/180*pi,NS1,EW1,X1,Y1,Z1); Cal((((C2/60)+B2)/60+A2)/180*pi,(((F2/60) +E2)/60+D2)/180*pi,NS2,EW2,X2,Y2,Z2); If (Abs(X1+X2)<1E-10) And (Abs(Y1+Y2)<1E-10) And (Abs(Z1+Z2)<1E- 10) Then Alpha:=pi Else Begin SqrD:=Sqr(X1-X2)+Sqr(Y1-Y2)+Sqr(Z1-Z2); Alpha:=Arctan(Sqrt(SqrD)/2/Sqrt(1-SqrD/4))*2; End; L:=Alpha*6875/2; Write('The distance to the iceberg: '); Write(L:0:2); WriteLn(' miles.'); If Round(L*100)<10000 Then WriteLn('DANGER!'); End. Message #488. Received at 18:15:23. Current ship's coordinates are 45^00'00" SL and 45^00'00" WL. An iceberg was noticed at 45^00'00" NL and 45^00'00" WL. === Message #488. Received at 18:15:23. Current ship's coordinates are 41^46'00" NL and 50^14'00" WL. An iceberg was noticed at 41^14'11" NL and 51^09'00" WL. === Message #488. Received at 18:15:23. Current ship's coordinates are 45^00'00" SL and 45^00'00" WL. An iceberg was noticed at 45^00'00" NL and 135^00'00" EL. === Message #488. Received at 18:15:23. Current ship's coordinates are 90^00'00" NL and 50^14'00" WL. An iceberg was noticed at 90^00'00" SL and 151^09'00" WL. === Message #488. Received at 18:15:23. Current ship's coordinates are 0^00'00" NL and 50^00'00" WL. An iceberg was noticed at 0^00'00" SL and 130^00'00" EL. === Message #488. Received at 18:15:23. Current ship's coordinates are 40^00'00" NL and 1^00'00" WL. An iceberg was noticed at 40^00'00" NL and 1^00'00" EL. === Message #488. Received at 18:15:23. Current ship's coordinates are 40^09'18" NL and 1^00'01" WL. An iceberg was noticed at 40^50'11" NL and 1^00'00" EL. === Message #488. Received at 18:15:23. Current ship's coordinates are 40^09'19" NL and 1^00'01" WL. An iceberg was noticed at 40^50'12" NL and 1^00'00" EL. === The distance to the iceberg: 5399.61 miles. The distance to the iceberg: 52.04 miles. DANGER! The distance to the iceberg: 7199.48 miles. The distance to the iceberg: 10799.22 miles. The distance to the iceberg: 4799.66 miles. The distance to the iceberg: 91.92 miles. DANGER! The distance to the iceberg: 100.00 miles. The distance to the iceberg: 99.99 miles. DANGER! > The distance to the iceberg: 5399.61 miles. > The distance to the iceberg: 52.04 miles. > DANGER! > The distance to the iceberg: 7199.48 miles. > The distance to the iceberg: 10799.22 miles. > The distance to the iceberg: 4799.66 miles. > The distance to the iceberg: 91.92 miles. > DANGER! > The distance to the iceberg: 100.00 miles. > The distance to the iceberg: 99.99 miles. > DANGER! > > The distance to the iceberg: 5399.61 miles. > The distance to the iceberg: 52.04 miles. > DANGER! > The distance to the iceberg: 7199.48 miles. > The distance to the iceberg: 10799.22 miles. > The distance to the iceberg: 4799.66 miles. > The distance to the iceberg: 91.92 miles. > DANGER! > The distance to the iceberg: 100.00 miles. > The distance to the iceberg: 99.99 miles. > DANGER! > Look carefully at test : Message #488. Received at 18:15:23. Current ship's coordinates are 45^00'00" SL and 45^00'00" WL. An iceberg was noticed at 45^00'00" NL and 135^00'00" EL. === My answer is 10799.22 AC program gives answers The distance to the iceberg: 5399.61 miles. The distance to the iceberg: 52.04 miles. DANGER! The distance to the iceberg: 10799.22 miles. The distance to the iceberg: 10799.22 miles. The distance to the iceberg: 10799.22 miles. The distance to the iceberg: 91.92 miles. DANGER! The distance to the iceberg: 100.00 miles. The distance to the iceberg: 99.99 miles. DANGER! This is my source code: #include <stdio.h> #include <math.h> #include <string.h> double w1,l1; double w2,l2; double x1,x2,x3; double Y1,Y2,Y3; const double r = 3437.5; const double pi = 3.14159265358979323846; int scan(void) { int num, hh, mm, ss; double x1,x2,x3; char str[5]; if(scanf("Message #%d.\n", &num) <= 0) return 0; scanf("Received at %d:%d:%d.\n", &hh, &mm, &ss); scanf("Current ship’s coordinates are\n"); scanf("%lf^%lf'%lf\" %s\n", &x1, &x2, &x3, str); if( str[0] == 'S' ) { x1=-x1; x2=-x2; x3=-x3; } w1 = (x1+x2/60+x3/3600)/180*pi; scanf("and %lf^%lf'%lf\" %s\n", &x1, &x2, &x3, str); if( str[0] == 'E' ) { x1=-x1; x2=-x2; x3=-x3; } l1 = (x1+x2/60+x3/3600)/180*pi; scanf("An iceberg was noticed at\n"); scanf("%lf^%lf'%lf\" %s\n", &x1, &x2, &x3, str); if( str[0] == 'S') { x1=-x1; x2=-x2; x3=-x3; } w2 = (x1+x2/60+x3/3600)/180*pi; scanf("and %lf^%lf'%lf\" %s\n", &x1, &x2, &x3, str); if( str[0] == 'E') { x1=-x1; x2=-x2; x3=-x3; } l2 = (x1+x2/60+x3/3600)/180*pi; scanf("===\n"); return 1; } void coord(double w, double l, double& a1, double& a2, double& a3) { a1 = cos(w)*cos(l); a2 = cos(w)*sin(l); a3 = sin(w); } double dist2(double a1, double a2, double a3, double b1, double b2, double b3) { return (a1-b1)*(a1-b1)+(a2-b2)*(a2-b2)+(a3-b3)*(a3-b3); } double doit(void) { coord(w1,l1, x1, x2, x3); coord(w2,l2, Y1, Y2, Y3); double res = r * acos (x1*Y1 + x2*Y2 + x3*Y3); return res; /* double x = dist2(x1,x2,x3,Y1,Y2,Y3); double cosfi = 1-x/2; double fi = acos(cosfi); return fi*r; */ } int main(void) { #ifndef ONLINE_JUDGE freopen("1030.in", "r", stdin); #endif while(scan()) { double res = doit(); if ((unsigned)printf("The distance to the iceberg: % 0.2lf miles.\n",res) < strlen("The distance to the iceberg: 100.00 miles.\n") ) printf("DANGER!\n"); /* printf("The distance to the iceberg: %.2lf miles.\n", res); if(res<99.995) printf("DANGER!\n"); */ } #ifndef ONLINE_JUDGE fclose(stdin); #endif return 0; } I have read all comments below, but my program still gets WA... #include <stdio.h> #include <math.h> int main () { int hh, dd, mm, danger, n; double x1, x2, x3, y1, y2, y3, a1, a2, a3, b1, b2, b3; char ns1, ns2, we1, we2; double x, y, z, a, b, c, res, Phi, Psi; char s1[3], s2[3], s3[3]; const double R = 6875; const double Pi = 3.1415926; char s [100]; int q = 1; while (q) { if (scanf ("Message #%d.\n", &n) == EOF) break; scanf ("Received at %2d:%2d:%2d.\n", &hh, &dd, &mm); scanf ("%[^\n]\n", s); scanf ("%2lf^%2lf'%2lf%c ", &x1, &x2, &x3, &c); scanf ("%cL\n", &ns1); scanf ("and %2lf^%2lf'%2lf%c ", &y1, &y2, &y3, &c); scanf ("%cL.\n", &we1); scanf ("%[^\n]\n", s); scanf ("%2lf^%2lf'%2lf%c ", &a1, &a2, &a3, &c); scanf ("%cL\n", &ns2); scanf ("and %2lf^%2lf'%2lf%c ", &b1, &b2, &b3, &c); scanf ("%cL.\n", &we2); scanf ("%[=]", s); if (scanf ("\n") == EOF) q = 0; Phi = Pi / 180 * (x1 + x2 / 60 + x3 / 3600); if (ns1 == 'S') Phi *= -1; Psi = Pi / 180 * (y1 + y2 / 60 + y3 / 3600); if (we1 == 'E') Psi *= -1; x = cos (Phi) * cos (Psi); y = cos (Phi) * sin (Psi); z = sin (Phi); Phi = Pi / 180 * (a1 + a2 / 60 + a3 / 3600); if (ns2 == 'S') Phi *= -1; Psi = Pi / 180 * (b1 + b2 / 60 + b3 / 3600); if (we2 == 'E') Psi *= -1; a = cos (Phi) * cos (Psi); b = cos (Phi) * sin (Psi); c = sin (Phi); res = R * acos (a*x + b*y + c*z) / 2; danger = (res < 99.995); if (res < 0.005) res = 0; printf ("The distance to the iceberg: %.2lf miles.\n", res); if (danger) printf ("DANGER!\n"); }; return 0; } 1) A test can have more than one message to be processed ? 2) Are you sure there is nothing wrong with the data set ? var st,st1,st2,st3,st4 :string; x,y,z,v,k :array[1..2] of extended; r,kc :extended; procedure doc(var st :string; var t :extended); begin t := 0; while ( st[1] >= '0') and ( st[1] <= '9' ) do begin t := t * 10 + ord( st[1] ) - 48; delete(st,1,1); end; delete(st,1,1); end; procedure doc1(var st :string; var t :extended); var x,y,z :extended; begin while ( st[1] < '0' ) or ( st[1] > '9' ) do delete(st,1,1); doc(st, x); doc(st,y); doc(st, z); t := x + y/60 + z/3600; t := t * pi / 180; if ( pos('SL',st) > 0 ) or ( pos('WL',st) > 0 ) then t := -t; end; procedure input; begin readln( st); readln( st); readln( st); readln( st1); readln( st2); readln( st); readln( st3); readln( st4); doc1(st1, v[1]); doc1(st2, k[1]); doc1(st3, v[2]); doc1(st4, k[2]); end; procedure tinh_toa_do; begin r := 6875/2; x[1] := r * sin( v[1] ); y[1] := r * cos( v[1] ) * sin( k[1] ); z[1] := r * cos( v[1] ) * cos( k[1] ); x[2] := r * sin( v[2] ); y[2] := r * cos( v[2] ) * sin( k[2] ); z[2] := r * cos( v[2] ) * cos( k[2] ); end; procedure tinh_khoang_cach; var canh,co,goc :extended; begin canh := sqrt( sqr( x[1] - x[2] ) + sqr( y[1] - y[2] ) + sqr( z[1] - z[2] ) ); co := ( 2*sqr(r) - sqr(canh) ) / ( 2 * sqr(r) ); if co = 0 then goc := pi/2 else goc := arctan( sqrt( 1/sqr(co) - 1 ) ); kc := r * goc; end; procedure solve; begin tinh_toa_do; tinh_khoang_cach; end; procedure out; begin writeln('The distance to the iceberg: ',kc:0:2,' miles.'); if kc < 99.95 then writeln('DANGER!'); end; begin input; solve; out; end. Maybe the input is wrong but i don't know how to do this help thanks ! #include<iostream.h> #include<math.h> #include<stdio.h> const double pi=3.1415926; const double R=3437.5; double x_ship,y_ship,z_ship; double x_ice,y_ice,z_ice; double dist(double x1,double y1,double z1,double x2,double y2,double z2) { double ans; ans=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2)); return ans; } void main() { double X1,X2,X3,Y1,Y2,Y3; double A1,A2,A3,B1,B2,B3; double ans,sita,d,r1,r2; int location_ship1,location_ship2,location_ice1,location_ice2; char ch[200]; char c; //north=1;south=-1;east=1;west=-1; //input gets(ch); gets(ch); gets(ch); cin>>X1>>c>>X2>>c>>X3>>c>>ch; if(ch[0]=='N') location_ship1=1; else location_ship1=-1; cin>>ch>>Y1>>c>>Y2>>c>>Y3>>c>>ch; if(ch[0]=='E') location_ship2=1; else location_ship2=-1; gets(ch); cin>>A1>>c>>A2>>c>>A3>>c>>ch; if(ch[0]=='N') location_ice1=1; else location_ice1=-1; cin>>ch>>B1>>c>>B2>>c>>B3>>c>>ch; if(ch[0]=='E') location_ice2=1; else location_ice2=-1; gets(ch);
z_ship=R*sin((X1+(X2+X3/60)/60)*pi/180); z_ship*=location_ship1; r1=R*cos((X1+(X2+X3/60)/60)*pi/180); z_ice=R*sin((A1+(A2+A3/60)/60)*pi/180); z_ice*=location_ice1; r2=R*cos((A1+(A2+A3/60)/60)*pi/180); y_ship=r1*cos((Y1+(Y2+Y3/60)/60)*pi/180); y_ice=r2*cos((B1+(B2+B3/60)/60)*pi/180); x_ship=r1*sin((Y1+(Y2+Y3/60)/60)*pi/180); x_ship*=location_ship2; x_ice=r2*sin((B1+(B2+B3/60)/60)*pi/180); x_ice*=location_ice2; d=dist(x_ship,y_ship,z_ship,x_ice,y_ice,z_ice); sita=acos((R*R*2-d*d)/(2*R*R)); ans=R*sita; printf("The distance to the iceberg: %.2lf miles. \n",ans); if(ans<100) printf("DANGER!\n"); } Your program prints DANGER if the answer is 99.999999; you should print DANGER only if the number you have printed is 99.99 or less Good luck. thank you but i could not correct my program and i still got the wrong answer whether you can tell me more also i doubt whether my input code is ok? i am so lazy and i do in this way Well, you could see how many symbols you have printed (i think printf returns this), so if this number is ... (one more than usually), you print 'DANGER!' Good luck. #include <math.h> #include <stdio.h> #include <string.h> #define DIAMETER 6875.0 #define CIRCLE_PI 3.1415926536 typedef struct { int sign; int deg; int min; int sec; } degree; typedef struct { degree latitude; degree longitude; } coordinate; typedef struct { double x; double y; double z; } vector; coordinate ship; coordinate iceberg; double dist; void str_to_coord(degree *ptr,char *p,char positive) { while(*p<'0'||*p>'9') p++; ptr->deg=0; while(*p>='0'&&*p<='9') ptr->deg=ptr->deg*10+(*(p++)-'0'); while(*p<'0'||*p>'9') p++; ptr->min=0; while(*p>='0'&&*p<='9') ptr->min=ptr->min*10+(*(p++)-'0'); while(*p<'0'||*p>'9') p++; ptr->sec=0; while(*p>='0'&&*p<='9') ptr->sec=ptr->sec*10+(*(p++)-'0'); while(*p<'A'||*p>'Z') p++; ptr->sign=(*p==positive?1:-1); } void init() { char buf[80]; gets(buf); gets(buf); gets(buf); gets(buf); str_to_coord(&(ship.latitude),buf,'N'); gets(buf); str_to_coord(&(ship.longitude),buf,'E'); gets(buf); gets(buf); str_to_coord(&(iceberg.latitude),buf,'N'); gets(buf); str_to_coord(&(iceberg.longitude),buf,'E'); } double deg_to_rad(degree angle) { double res; res=(double)angle.deg+(double)angle.min/60.0+(double) angle.sec/3600.0; res=res/180.0*CIRCLE_PI; if(angle.sign==-1) res=-res; return res; } void coord_to_vec(vector *vec,coordinate coord) { double phi,theta; phi=deg_to_rad(coord.latitude); theta=deg_to_rad(coord.longitude); vec->x=cos(phi)*cos(theta)*DIAMETER/2.0; vec->y=cos(phi)*sin(theta)*DIAMETER/2.0; vec->z=sin(phi)*DIAMETER/2.0; } double distance(vector a,vector b) { double t1=a.x-b.x; double t2=a.y-b.y; double t3=a.z-b.z; t1=t1*t1; t2=t2*t2; t3=t3*t3; return sqrt(t1+t2+t3); } void solve() { vector a,b; double t; coord_to_vec(&a,ship); coord_to_vec(&b,iceberg); t=distance(a,b); dist=acos(1.0-(t*t)/(2.0*(DIAMETER/2.0)*(DIAMETER/2.0)))* (DIAMETER/2.0); } void done() { printf("The distance to the iceberg: %0.2lf miles.\n",dist); if(floor(dist*100.0+0.5)<10000.0) printf("DANGER!\n"); } main() { init(); solve(); done(); return 0; } I think the problem is with this; if(floor(dist*100.0+0.5)<10000.0) printf("DANGER!\n"); i can make a test in which the answer is much nearer to 100 than you expect; one way to correct this: #include <string.h> ... void done() { if ( printf("The distance to the iceberg: %0.2lf miles.\n",dist) == strlen("The distance to the iceberg: 100.00 miles.\n") ) printf("DANGER!\n"); } Good luck. sorry, a mistake -- not == but < What's wrong here? All the formulas are abviously correct, but wwhy do I get WA? By the way, in some discussions on this problem the author (s) Li,Yi had the same problem, and somebody said "try :'if way < 99.95 then ...", but this doesn't work with my program as well. Program Titanic; Const di=6875; r=di/2; Var u1,u2,u3,v1,v2,v3:Longint; a,b:Extended; x1,y1,z1,x2,y2,z2:Extended; c:Char; s:String; d1,d2,d3:Extended; cs,sn,al:Extended; Function StrToInt(s:String):Longint; Var Cod:Integer; v:Longint; Begin Val(s,v,Cod); StrToInt:=v; End; Function RR(st:Char):String; Var s:String; c:Char; Begin s:=''; Repeat Read(c); If c In ['0'..'9'] Then s:=s+c; Until c=st; RR:=s; End; Function D(x1,y1,z1,x2,y2,z2:Extended):Extended; Begin D:=Sqrt(Sqr(x1-x2)+Sqr(y1-y2)+Sqr(z1-z2)); End; Function SSin(a:Extended):Extended; Begin SSin:=sin(a/180*Pi); End; Function CCos(a:Extended):Extended; Begin CCos:=cos(a/180*Pi); End; Begin While Not Eof Do Begin Readln; Readln; Readln; u1:=StrToInt(RR('^')); u2:=StrToInt(RR('''')); u3:=StrToInt(RR('"')); Readln(s); If Pos('N',s)>0 Then Begin u1:=-u1; u2:=-u2; u3:=-u3; End; v1:=StrToInt(RR('^')); v2:=StrToInt(RR('''')); v3:=StrToInt(RR('"')); Readln(s); If Pos('W',s)>0 Then Begin v1:=-v1; v2:=-v2; v3:=-v3; End; a:=u1+u2/60+u3/60/60; { a:=90-Abs(a);} b:=v1+v2/60+v3/60/60; x1:=ccos(a)*ccos(b); y1:=ccos(a)*ssin(b); z1:=ssin(a); Readln; u1:=StrToInt(RR('^')); u2:=StrToInt(RR('''')); u3:=StrToInt(RR('"')); Readln(s); If Pos('N',s)>0 Then Begin u1:=-u1; u2:=-u2; u3:=-u3; End; v1:=StrToInt(RR('^')); v2:=StrToInt(RR('''')); v3:=StrToInt(RR('"')); Readln(s); If Pos('W',s)>0 Then Begin v1:=-v1; v2:=-v2; v3:=-v3; End; a:=u1+u2/60+u3/60/60; { a:=90-Abs(a);} b:=v1+v2/60+v3/60/60; x2:=ccos(a)*ccos(b); y2:=ccos(a)*ssin(b); z2:=ssin(a); d1:=D(0,0,0,x1,y1,z1); d2:=D(0,0,0,x2,y2,z2); d3:=D(x2,y2,z2,x1,y1,z1); cs:=(d1*d1+d2*d2-d3*d3)/(2*d1*d2); sn:=Sqrt(Abs(1-cs*cs)); If cs=0 Then al:=Pi/2 Else al:=Arctan(sn/cs); If cs<0 Then al:=al+Pi; Writeln('The distance to the iceberg: ',al*r:0:2,' miles.'); If al*r<100 Then Writeln('DANGER!'); Readln; End; End. #include <fstream.h> #include <stdio.h> #include <math.h> #include <string.h> const double D = 6875.; const double R = 3437.5; const double C_PI = 3.14159265358979323846; int main () { double phi1, phi2, teta1, teta2; double degrees, minutes, seconds, sign; double x1,x2,y1,y2,z1,z2; char s[300]; cin.getline(s, 299); cin.getline(s, 299); cin.getline(s, 299); // read the ship coordinates cin.getline(s, 299); degrees = atof(s); char *p = strchr(s,'^'); minutes = atof(p + 1); p = strchr(s, '\''); seconds = atof(p + 1); sign = 0.; if (strstr(s, "SL") != NULL) sign = -1.; if (strstr(s, "NL") != NULL) sign = 1.; phi1 = sign * (degrees + minutes / 60. + seconds / 3600.); cin >> s; cin.getline(s, 299); degrees = atof(s); p = strchr(s, '^'); minutes = atof(p + 1); p = strchr(s, '\''); seconds = atof(p + 1); sign = 0.; if (strstr(s,"WL") != NULL) sign=-1.; if (strstr(s,"EL") != NULL) sign=1.; teta1 = sign * (degrees + minutes / 60. + seconds / 3600.); // skip one line cin.getline(s, 299); // read the iceberg coordinates cin.getline(s, 299); p = strchr(s,'^'); degrees = atof(s); minutes = atof(p+1); p = strchr(s, '\''); seconds = atof(p+1); sign = 0.; if (strstr(s, "SL") != NULL) sign=-1.; if (strstr(s, "NL") != NULL) sign=1.; phi2 = sign * (degrees + minutes / 60. + seconds / 3600.); cin >> s; cin.getline(s, 299); degrees = atof(s); p = strchr(s,'^'); minutes = atof(p + 1); p = strchr(s, '\''); seconds = atof(p + 1); sign = 0.; if (strstr(s, "WL") != NULL) sign = -1.; if (strstr(s, "EL") != NULL) sign = 1.; teta2 = sign * (degrees + minutes / 60. + seconds / 3600.); // calculate the euclids coordinate phi1 = phi1 * C_PI / 180.; phi2 = phi2 * C_PI / 180.; teta1 = teta1 * C_PI / 180.; teta2 = teta2 * C_PI / 180.; x1 = R * cos(phi1) * cos(teta1); y1 = R * cos(phi1) * sin(teta1); z1 = R * sin(phi1); x2 = R * cos(phi2) * cos(teta2); y2 = R * cos(phi2) * sin(teta2); z2 = R * sin(phi2); double dist = sqrt((x1-x2) * (x1-x2) + (y1-y2) * (y1-y2) + (z1-z2) * (z1-z2)); double k = dist / D; double way = D * asin(k); printf("The distance to the iceberg: %4.2lf miles.\n", way); if ( way < 100.00 ) printf("DANGER\n"); return 0; } Maybe you should try specifying your headers. > #include <fstream.h> > #include <stdio.h> > #include <math.h> > #include <string.h> > > const double D = 6875.; > const double R = 3437.5; > const double C_PI = 3.14159265358979323846; > > int main () > { > double phi1, phi2, teta1, teta2; > double degrees, minutes, seconds, sign; > double x1,x2,y1,y2,z1,z2; > char s[300]; > > cin.getline(s, 299); > cin.getline(s, 299); > cin.getline(s, 299); > // read the ship coordinates > cin.getline(s, 299); > > degrees = atof(s); > > char *p = strchr(s,'^'); > minutes = atof(p + 1); > > p = strchr(s, '\''); > seconds = atof(p + 1); > > sign = 0.; > if (strstr(s, "SL") != NULL) sign = -1.; > if (strstr(s, "NL") != NULL) sign = 1.; > > phi1 = sign * (degrees + minutes / 60. + seconds / 3600.); > > cin >> s; > cin.getline(s, 299); > > degrees = atof(s); > > p = strchr(s, '^'); > minutes = atof(p + 1); > > p = strchr(s, '\''); > seconds = atof(p + 1); > > sign = 0.; > if (strstr(s,"WL") != NULL) sign=-1.; > if (strstr(s,"EL") != NULL) sign=1.; > > teta1 = sign * (degrees + minutes / 60. + seconds / > 3600.); > > // skip one line > cin.getline(s, 299); > // read the iceberg coordinates > > cin.getline(s, 299); > p = strchr(s,'^'); > degrees = atof(s); > minutes = atof(p+1); > > p = strchr(s, '\''); > seconds = atof(p+1); > > sign = 0.; > if (strstr(s, "SL") != NULL) sign=-1.; > if (strstr(s, "NL") != NULL) sign=1.; > > phi2 = sign * (degrees + minutes / 60. + seconds / 3600.); > > cin >> s; > cin.getline(s, 299); > degrees = atof(s); > p = strchr(s,'^'); > minutes = atof(p + 1); > > p = strchr(s, '\''); > > seconds = atof(p + 1); > > sign = 0.; > if (strstr(s, "WL") != NULL) sign = -1.; > if (strstr(s, "EL") != NULL) sign = 1.; > teta2 = sign * (degrees + minutes / 60. + seconds / > 3600.); > > // calculate the euclids coordinate > phi1 = phi1 * C_PI / 180.; > phi2 = phi2 * C_PI / 180.; > teta1 = teta1 * C_PI / 180.; > teta2 = teta2 * C_PI / 180.; > > x1 = R * cos(phi1) * cos(teta1); > y1 = R * cos(phi1) * sin(teta1); > z1 = R * sin(phi1); > > x2 = R * cos(phi2) * cos(teta2); > y2 = R * cos(phi2) * sin(teta2); > z2 = R * sin(phi2); > > double dist = sqrt((x1-x2) * (x1-x2) + (y1-y2) * (y1-y2) > + (z1-z2) * (z1-z2)); > double k = dist / D; > double way = D * asin(k); > > printf("The distance to the iceberg: %4.2lf miles.\n", > way); > if ( way < 100.00 ) printf("DANGER\n"); > > return 0; > } > change 'way<100.00' to '100.00-way>0.005' then you will got accept; Good luck! I've got several WA on this problem. Could somebody tell me if I should use this equation to count : angle = acos(cos(as)*cos(bs)*cos(ai)*cos(bi)+cos(as)*sin(bs) *cos(ai)*sin(bi)+sin(as)*sin(ai)); where as,bs are altitude and longitude of ship, and ai,bi of iceberg ??? And could I use this: ang2 = [ ( (secs/60)+mins )/60 + hrs ] * PI/180 to get the radian angle from given coordinates (hh^mm'ss'') Please help!??! My answer: 6875*asin(sqrt((1-cos(x)*cos(a)*cos(y-b)-sin(x)*sin(a))/2)) x,y are the ship's coordinates, and a,b are the iceberg's. i think it's right ,but my sample ans is 52.14 WHY????????????? I thought that the longitude is East/West, and the latitude North-South. We are to determine if a floating-point number is precisely less than 100, i.e. if x < 100-eps. What precision should we use in order to allow some tolerance in calculations, and not to miss an input set? |
|