c++ - Ant in a Cuboid find shortest path: length, width and height of a cuboid are given. Output should display the shortest distance in floating point -


an ant saw box of sugar. climbs top corner of box inside. unfortunately, box has few sugar cubes @ bottom corner opposite ant. write program ant find shortest path reach sugar cubes.

input , ouptut format:

input consists of 3 integers corresponds length, width , height of cuboid. output should display shortest distance in floating point. correct 2 decimal places.

sample input , output:

enter length 5 enter width 6 enter height 7 shortest distance 14.81

the formula used minimum among (1) square root[(a+b)^2 + c^2] (2) square root[(b+c)^2 + a^2] (3) square root[(a+c)^2 + b^2]

the answer getting same inputs 13.42 going wrong?

    s1=x+y;     s2=y+z;     s3=x+z;    s1sq=s1*s1;    s2sq=s2*s2;    s3sq=s3*s3;    x2=pow(x,2.0);    y2=pow(y,2.0);    z2=pow(z,2.0);    full1=s1sq+z2;    full2=s2sq+x2;    full3=s3sq+y2;    sq1=sqrt(full1);    sq2=sqrt(full2);    sq3=sqrt(full3);    min=sq1;    if(sq1<sq2 && s1<sq3)    min=sq1;    if(sq2<sq3 && sq2<sq1)    min=sq2;    else min=sq3;    printf("%.2f",min);    return 0; 

you made mistake in formulae. way you're looking @ it, should write:

(a + sqrt(b^2 + c^2)) (b + sqrt(a^2 + c^2)) (c + sqrt(a^2 + b^2)). 

and then, wouldn't shortest distance. give example, suppose cube of 1x1x1 units sides along x, y , z axes. in order ant (0,0,0) (1,1,1), can go (0,0,0) -> (0,1,1) -> (1,1,1) or (0,0,0) -> (0, 0.5, 0.5) -> (1,1,1)

the second path shorter 1 obviously.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -