В конце замечаний на русском языке исправьте "для восточной/южной" на "для восточной/западной". Спасибо (долгота) Russian statements fixed. Thanks. You have to make one more "if" ###################################################################################### d = math.sin(p1) * math.sin(p2) + math.cos(p1) * math.cos(p2) * math.cos(abs(l1 - l2)) if str(d) == '-1.0000000000000002': d = int(d) d = math.acos(d) * 3437.5 ###################################################################################### there is a test where '-1.0000000000000002' in d so thats why acos gives error, you have to change '-1.0000000000000002' to -1 Edited by author 20.04.2023 21:49 Edited by author 20.04.2023 21:49 diameter of 6875 miles (/ 2 = 3437.5)??? the mean radius of earth is 6371.0 km (3958.8 miles) from sys import stdin, stdout, float_info import re from math import sin, cos, acos, fabs CONST_PI = acos(-1.0) CONST_EPSILON = 2.2204460492503130808472633361816 # CONST_PI = 3.141592653589793 data = [] def solve(a, b, c, d): a = a + (c / 3600) + (b / 60) # Checking the latitude and longitude position to determine positive pr negative degree if d == 'SL' or d == 'WL': a = -a a = a * CONST_PI / 180 return a def distComp(a): if fabs(a) < CONST_EPSILON: return 0 elif a > 0: return 1 else: return -1 # Input data = stdin.read().split() # # Test Input # data = ['Message', '#513.', 'Received', 'at', '22:30:11.', '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.', '==='] # data = ['Message', '#513.', 'Received', 'at', '22:30:11.', 'Current', "ship's", 'coordinates', 'are', '36^46\'00"', 'EL', # 'and', '50^14\'00"', 'WL.', 'An', 'iceberg', 'was', 'noticed', 'at', '41^14\'11"', 'NL', 'and', '76^09\'00"', 'WL.', '==='] # Message Received # messageReceived = [int(s) for s in re.findall(r'\b\d+\b', data[4])] # hh = messageReceived[0] # mm = messageReceived[1] # ss = messageReceived[2] # print('\nMessage Received at: ', hh, mm, ss) # Ship's Latitude shipLatitude = [float(s) for s in re.findall(r'\b\d+\b', data[9])] x1 = shipLatitude[0] x2 = shipLatitude[1] x3 = shipLatitude[2] x4 = data[10] # print('\nShip Latitude: ', x1, x2, x3, x4) # Ship's Longitude shipLongitude = [float(s) for s in re.findall(r'\b\d+\b', data[12])] y1 = shipLongitude[0] y2 = shipLongitude[1] y3 = shipLongitude[2] y4 = data[13][:2] # print('\nShip Longitude: ', y1, y2, y3, y4) # Ice Berg's Latitude iceBergLatitude = [float(s) for s in re.findall(r'\b\d+\b', data[19])] a1 = iceBergLatitude[0] a2 = iceBergLatitude[1] a3 = iceBergLatitude[2] a4 = data[20] # print('\nIce Berg Latitude: ', a1, a2, a3, a4) # Ice Berg's Longitude iceBergLongitude = [float(s) for s in re.findall(r'\b\d+\b', data[22])] b1 = iceBergLongitude[0] b2 = iceBergLongitude[1] b3 = iceBergLongitude[2] b4 = data[23][:2] # print('\nIce Berg Longitude: ', b1, b2, b3, b4) shipLatitudeResult = solve(x1, x2, x3, x4) shipLongitudeResult = solve(y1, y2, y3, y4) iceBergLatitudeResult = solve(a1, a2, a3, a4) iceBergLongitudeResult = solve(b1, b2, b3, b4) dist = 6875.0/2 dist = acos(sin(shipLatitudeResult) * sin(iceBergLatitudeResult) + cos(shipLatitudeResult) * cos(iceBergLatitudeResult) * cos(shipLongitudeResult - iceBergLongitudeResult)) * dist print('\nThe distance to the iceberg:', round(dist, 2), 'miles.') if dist < 100: print('DANGER!') Edited by author 30.03.2021 16:37 Hello! I`ve got a code, equations must be right, but the result is different from sample. I don`t know what the problem is. Please, help!! My result is 45,28, whereas the sample`s one is 52,04. static void Main(string[] args) { const double D = 6875; const double pi = 3.1415926535897932384626433; string trash; char[] separators = new[] { ' ', '^', '"', '\''}; /* double shipx, shipy, shipz, icex, icey, icez; */ for (int i = 0; i < 3; i++) trash = Console.ReadLine(); var shipLat = Console.ReadLine().Split(separators); var shipLong = Console.ReadLine().Split(separators); trash = Console.ReadLine(); var iceLat = Console.ReadLine().Split(separators); var iceLong = Console.ReadLine().Split(separators); trash = Console.ReadLine(); double phi1 = (int.Parse(shipLat[0]) + (int.Parse(shipLat[1]) + int.Parse(shipLat[2]) / 60) / 60) * (pi / 180); double phi2 = (int.Parse(shipLong[1]) + (int.Parse(shipLong[2]) + int.Parse(shipLong[3]) / 60) / 60) * (pi / 180); if (shipLat[3] == "SL") phi1 = -phi1; if (shipLong[4] == "WL") phi2 = -phi2; double L1 = (int.Parse(iceLat[0]) + (int.Parse(iceLat[1]) + int.Parse(iceLat[2]) / 60)/60) * (pi / 180); double L2 = (int.Parse(iceLong[1]) + (int.Parse(iceLong[2]) + int.Parse(iceLong[3]) / 60)/60) * (pi / 180); if (iceLat[3] == "SL") L1 = -L1; if (iceLong[4] == "WL") L2 = -L2; double ans = Math.Acos(Math.Sin(phi1) * Math.Sin(L1) + Math.Cos(phi1) * Math.Cos(L1) * Math.Cos(L2-phi2)); double dist = ans * D/2; Console.Write("The distance to the iceberg: "); Console.WriteLine("{0:0.00}", dist); if (100.00 - dist > 0.005) Console.WriteLine("DANGER!"); } The problem is solved. It`s impossible to have the right answer with INT parsing. You need to use double. Hello, everyone. Just wanted to summarize some info about this problem. Ok, my code got AC with following things in it: 1. My pi was 3.1415926535897932384626433 (perhaps it is enough) 2. I used this formula from wikipedia deltaArc=acos(sin(phiA)*sin(phiB)+cos(phiA)*cos(phiB)*cos(deltaL)); distance = deltaArc*3437.5; (see the first formula from wikipedia http://en.wikipedia.org/wiki/Great-circle_distance)3. I used the following condition: if(100.00-distance>0.005) printf("DANGER!\n"); Hope it will help somebody. thanks a lot ...your formula is really useful.^_^ Used everynth what is written here. However still WA8. Any new ideas?... Do u know that pi can be calculated by this formula: atan(1) * 4? Всем привет. Пишу на с++. И возникла проблема с вводом широты и долготы без перехода на новую строку. Выходит: <X1> ^<X2> '... что не очень красиво. Хотелось бы так <X1>^<X2>'<X3>" <NL/SL> но не знаю как реализовать. Спасибо. scanf("%d^%d'%d\" %cL", &x1, &x2, &x3, &ch); Edited by author 07.03.2010 10:26 Hi, Alright guys I have solved the problem. Being a newbie to c# caused me to submit program 35 times. I didn't realize that Math.Round() function will change 100.00 to 100 instead. Here are the lines which I changed. double miles = distance(ship_latitude_radian, ship_longitude_radian, iceberg_latitude_radian, iceberg_longitude_radian); if (Math.Round(miles, 2) < 100.00) { Console.WriteLine("The distance to the iceberg: {0:F2} miles.", miles); Console.WriteLine("DANGER!"); } else { Console.WriteLine("The distance to the iceberg: {0:F2} miles.", miles); } Solved by making the following comparison in java: if (Math.round(distance*100) < 10000) { System.out.println("DANGER!"); } it will force it to compare with 2 digits precision. #include <iostream> #include <math.h> using namespace std; const long double D=6875; const long double pi=3.1415926535897932384626433; struct postype{ long double x,y,z; }; int x1,x2,x3,yy1,y2,y3; int sx1,sx2,sx3,syy1,sy2,sy3; char str[100000]; char xstr[10],ystr[10],sxstr[10],systr[10]; postype ship,ice; long double angle1,angle2;
long double dis(double a,double b,double c,double d,double e,double f){ a-=d; b-=e; c-=f; return sqrt(a*a+b*b+c*c); }
int main(){ for (int i=1;i<=3;++i) gets(str); scanf("%d^%d'%d\" %s\n",&x1,&x2,&x3,&xstr); scanf("and %d^%d'%d\" %s\n",&yy1,&y2,&y3,&ystr); gets(str); scanf("%d^%d'%d\" %s\n",&sx1,&sx2,&sx3,&sxstr); scanf("and %d^%d'%d\" %s\n",&syy1,&sy2,&sy3,&systr); gets(str);
double a=x1,b=x2,c=x3; angle1=a*3600+b*60.0+c; angle1/=3600.0*180; angle1*=pi; if (xstr[0]=='S') angle1=-angle1; a=yy1;b=y2;c=y3; angle2=a*3600+b*60.0+c; angle2/=3600.0*180; angle2*=pi; if (ystr[0]=='W') angle2=-angle2; ship.x=D/2*cos(angle1)*cos(angle2); ship.y=D/2*cos(angle1)*sin(angle2); ship.z=D/2*sin(angle1);
a=sx1,b=sx2,c=sx3; angle1=a*3600+b*60+c; angle1/=3600.0*180; angle1*=pi; if (sxstr[0]=='S') angle1=-angle1; a=syy1;b=sy2;c=sy3; angle2=a*3600+b*60+c; angle2/=3600.0*180; angle2*=pi; if (systr[0]=='W') angle2=-angle2; ice.x=D/2*cos(angle1)*cos(angle2); ice.y=D/2*cos(angle1)*sin(angle2); ice.z=D/2*sin(angle1);
long double linedis=dis(ship.x,ship.y,ship.z,ice.x,ice.y,ice.z); linedis/=2; long double AA=asin(linedis*2/D)*2; long double ans=AA; ans=ans*D/2;
printf("The distance to the iceberg: "); cout.precision(2); cout<<fixed; cout<<ans; printf(" miles.\n"); if (100.00-ans>0.005) printf("DANGER!"); } just because R=6875*(1.0/2.0) instead of R=6875/2 hope it's helpful to you. {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! 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 Can anyone give me a test?Thanks a lot. I aways WA,but I cannot find the error.Can someone help me? #include<stdio.h> #include<math.h> #include<algorithm> using namespace std; #define PI 3.1415926535897932384626433 int lat1, lat2, lat3, lon1, lon2, lon3, trash; char latc, lonc; long double latf, lats, lonf, lons; long double sq(double x){ return x*x; } int main(void){ scanf("Message #%d.\nReceived at %d:%d:%d.\nCurrent ship’s coordinates are\n%d^%d'%d\" %cL\nand %d^%d'%d\" %cL.\n", &trash, &trash, &trash, &trash, &lat1, &lat2, &lat3, &latc, &lon1, &lon2, &lon3, &lonc); lats = (latc=='N'?1:-1)*(lat1 + (lat2 + lat3/60.0)/60.0)*PI/180.0; lons = (lonc=='W'?-1:1)*(lon1 + (lon2 + lon3/60.0)/60.0)*PI/180.0; scanf("An iceberg was noticed at\n%d^%d'%d\" %cL\nand %d^%d'%d\" %cL.", &lat1, &lat2, &lat3, &latc, &lon1, &lon2, &lon3, &lonc); latf = (latc=='N'?1:-1)*(lat1 + (lat2 + lat3/60.0)/60.0)*PI/180.0; lonf = (lonc=='W'?-1:1)*(lon1 + (lon2 + lon3/60.0)/60.0)*PI/180.0; double dlon = lonf - lons, dlat = latf - lats; double a = acos(sin(lats)*sin(latf) + cos(lats)*cos(latf)*cos(dlon)); double len = 3437.5*a; printf("The distance to the iceberg: %.2lf miles.\n", len); if(100.00-len>0.005) printf("DANGER!\n"); return 0; } Am I wright about input? Have you debugged locally? Change the word "ship’s" to "ship's" and you'll get AC. Anyway, your algorithm is amazing. For example, you even don't use epsilon in "if(100.00-len>0.005)"... Very impressive. import java.util.*; import java.text.*; public class zad1030 { public static void main(String[] args) { double alpS1, alpS2, alpD1, alpD2, l, r=6875/2.0, a, b, c; String s, res, sh="0123456789"; Scanner cin=new Scanner(System.in); NumberFormat f=NumberFormat.getInstance(Locale.UK); f.setMinimumFractionDigits(2); f.setMaximumFractionDigits(2); while (cin.hasNext()) { cin.nextLine(); cin.nextLine(); cin.nextLine(); s=cin.nextLine(); while (sh.indexOf(s.substring(0, 1))<0) { s=s.substring(1); } a=Integer.parseInt(s.substring(0, s.indexOf("^"))); s=s.substring(s.indexOf("^")+1); b=Integer.parseInt(s.substring(0, s.indexOf("'"))); s=s.substring(s.indexOf("'")+1); c=Integer.parseInt(s.substring(0, s.indexOf("\""))); s=s.substring(s.indexOf("\"")+1); alpS1=Math.toRadians(a+b/60.0+c/3600.0); if (s.substring(0, 3)==" SL") alpS1=-alpS1; s=cin.nextLine(); while (sh.indexOf(s.substring(0, 1))<0) { s=s.substring(1); } a=Integer.parseInt(s.substring(0, s.indexOf("^"))); s=s.substring(s.indexOf("^")+1); b=Integer.parseInt(s.substring(0, s.indexOf("'"))); s=s.substring(s.indexOf("'")+1); c=Integer.parseInt(s.substring(0, s.indexOf("\""))); s=s.substring(s.indexOf("\"")+1); alpD1=Math.toRadians(a+b/60.0+c/3600.0); if (s.substring(0, 3)==" WL") alpD1=-alpD1; cin.nextLine(); s=cin.nextLine(); while (sh.indexOf(s.substring(0, 1))<0) { s=s.substring(1); } a=Integer.parseInt(s.substring(0, s.indexOf("^"))); s=s.substring(s.indexOf("^")+1); b=Integer.parseInt(s.substring(0, s.indexOf("'"))); s=s.substring(s.indexOf("'")+1); c=Integer.parseInt(s.substring(0, s.indexOf("\""))); s=s.substring(s.indexOf("\"")+1); alpS2=Math.toRadians(a+b/60.0+c/3600.0); if (s.substring(0, 3)==" SL") alpS2=-alpS2; s=cin.nextLine(); while (sh.indexOf(s.substring(0, 1))<0) { s=s.substring(1); } a=Integer.parseInt(s.substring(0, s.indexOf("^"))); s=s.substring(s.indexOf("^")+1); b=Integer.parseInt(s.substring(0, s.indexOf("'"))); s=s.substring(s.indexOf("'")+1); c=Integer.parseInt(s.substring(0, s.indexOf("\""))); s=s.substring(s.indexOf("\"")+1); alpD2=Math.toRadians(a+b/60.0+c/3600.0); if (s.substring(0, 3)==" WL") alpD2=-alpD2; l=r*Math.acos(Math.sin(alpS1)*Math.sin(alpS2)+Math.cos(alpS1)*Math.cos(alpS2)*Math.cos(alpD1-alpD2)); l=Math.round(l*100)/100.0; res=f.format(l); System.out.println("The distance to the iceberg: "+res+" miles."); if (100.0-l>=0.01) System.out.println("DANGER!"); cin.nextLine(); } } } Edited by author 12.02.2010 21:06 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 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????????????? why my answer is 52.14? my code is: program titanic; uses math; const r=3437.5; var s:char; a,b,c,x,y,z,d,e,u,v,w:extended; begin assign(input,'titanic.in'); assign(output,'titanic.out'); reset(input); rewrite(output); readln; readln; readln; read(s); while s<>'^' do begin a:=a*10+ord(s)-48; read(s); end; read(s); while s<>chr(39) do begin z:=z*10+ord(s)-48; read(s); end; a:=a+z/60; z:=0; read(s); while s<>'"' do begin z:=z*10+ord(s)-48; read(s); end; read(s,s); a:=a+z/3600; if s='S' then a:=-a; readln; read(s,s,s,s,s); while s<>'^' do begin b:=b*10+ord(s)-48; read(s); end; read(s); z:=0; while s<>chr(39) do begin z:=z*10+ord(s)-48; read(s); end; b:=b+z/60; z:=0; read(s); while s<>'"' do begin z:=z*10+ord(s)-48; read(s); end; read(s,s); b:=b+z/3600; if s='W' then b:=-b; readln; readln; read(s); while s<>'^' do begin x:=x*10+ord(s)-48; read(s); end; read(s); z:=0; while s<>chr(39) do begin z:=z*10+ord(s)-48; read(s); end; x:=x+z/60; z:=0; read(s); while s<>'"' do begin z:=z*10+ord(s)-48; read(s); end; read(s,s); x:=x+z/3600; if s='S' then x:=-x; readln; read(s,s,s,s,s); while s<>'^' do begin y:=y*10+ord(s)-48; read(s); end; read(s); z:=0; while s<>chr(39) do begin z:=z*10+ord(s)-48; read(s); end; y:=y+z/60; z:=0; read(s); while s<>'"' do begin z:=z*10+ord(s)-48; read(s); end; read(s,s); y:=y+z/3600; if s='W' then y:=-y; c:=sin(a); z:=cos(a); d:=z*cos(b); e:=z*sin(b); u:=sin(x); z:=cos(x); v:=z*cos(y); w:=z*sin(y); z:=c*u+d*v+e*w; z:=r*pi*arccos(z)/180; writeln('The distance to the iceberg: ',z:0:8,' miles.'); if z<99.995 then writeln('DANGER!'); end. The main idea is: w1,w2,l1,l2. r:=6875/2; dist:=r*arccos(sin(w1)*sin(w2)+cos(w1)*cos(w2)*cos(l2-l1)); I had a problem with parsing the input. It turns out that my logic is fine but got WA3 because it is the first test where the longtitude has 3 digits degrees value. I don`t remember Pascal. In C expression 6875/2 means 3437, not 3437.5 |
|