Solution of 10773 - Back to Intermediate Math

Problem Description
source:https://uva.onlinejudge.org/external/107/10773.html

Umm! So, you claim yourself as an intelligent one? Let me check. As, computer science students always insist on optimization whenever possible, I give you an elementary problem of math to optimize. 
        You are trying to cross a river of width d meters. You are given that, the river flows at v ms−1 and you know that you can speed up the boat in u ms−1 . There may be two goals how to cross the river: One goal (called fastest path) is to cross it in fastest time, and it does not matter how far the flow of the river takes the boat. The other goal (called shortest path) is to steer the boat in a direction so that the flow of the river doesn’t take the boat away, and the boat passes the river in a line perpendicular to the boarder of the river. Is it always possible to have two different paths, one to pass at shortest time and the other at shortest path? If possible then, what is the difference (Let P s) between the times needed to cross the river in the different ways?

Input

The first line in the input file is an integer representing the number of test cases. Each of the test cases follows below. Each case consists three real numbers (all are nonnegative, d is positive) denoting the value of d, v and u respectively.

Output 

For each test case, first print the serial number of the case, a colon, an space and then print ‘can’t determine’ (without the quotes) if it is not possible to find different paths as stated above, else print the value of P corrected to three digits after decimal point. Check the sample input & output.

Sample Input 


8 5 6 
1 2 3 
1 5 6 

Sample Output 

Case 1: 1.079 
Case 2: 0.114 
Case 3: 0.135

Solution:
#include<stdio.h>
#include<math.h>

int main()
{
   double d,v,u,r1,r2,angle,s1,t,t1,t2,x;
  int n,i;
  while(scanf("%d",&n)==1)
  {
  for(i=1;i<=n;i++)
  {
        scanf("%lf%lf%lf",&d,&v,&u);
   if(d==0 || v==0 || u<=v)
     printf("Case %d: can't determine\n",i);
  else
   {
     r1=sqrt(u*u + v*v);
     x=v/u;
     angle= atan(x);
     s1=d/cos(angle);
     t1=s1/r1;
   
     r2=sqrt(u*u - v*v);
     t2=d/r2;
     if(t1>t2)
           t=t1-t2;
     else
              t=t2-t1;
     if(t<0.001)
          printf("Case %d: can't determine\n",i);
     else
      printf("Case %d: %.3lf\n",i,t);
     }
    
   }
 return 0; 
 }
 return 0;
}
image

No comments:

Post a Comment

Write your comment - Share Knowledge and Experience