Page 2 В конце замечаний на русском языке исправьте "для восточной/южной" на "для восточной/западной". Спасибо (долгота) 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. Всем привет. Пишу на с++. И возникла проблема с вводом широты и долготы без перехода на новую строку. Выходит: <X1> ^<X2> '... что не очень красиво. Хотелось бы так <X1>^<X2>'<X3>" <NL/SL> но не знаю как реализовать. Спасибо. scanf("%d^%d'%d\" %cL", &x1, &x2, &x3, &ch); #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. 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 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. 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. 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 when I define PI 3.1415926535897932384626 WA#5 but after 3 nums added #define PI 3.1415926535897932384626433, it's AC const double pi = acos(-1.0) 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? I have Crash(floating-point stack check). But I don't know what does it mean. I think that mistake in read input but I can't find it. My test always work. Who can help me? Edited by author 07.08.2006 12:04 Hi! If you haven't any sqrt(x):x<0, acos(a):a<-1|a>1, a/b:b=0 send me your code, i'll try to help you. PS: I've never got smth like "Crash(floating-point stack check)" so anyway send me code, pls, it's very interesting :) Edited by author 07.08.2006 12:16 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 |
|