EXPCODE2 - Editorial

basic-math
distance
easy
editorial
maths

#1

PROBLEM LINK:[https://www.codechef.com/problems/EXPCODE2][1]

Author:[https://www.codechef.com/users/vivek96][2]

DIFFICULTY:EASY

PREREQUISITES:Basic Maths,Math Functions

PROBLEM:
Chef want to book Ola Cab,he have to take ride from point one (xi,yi) to second point (xj,yj),Ola cost N rupees per unit, Chef have R rupees in his pocket,
You have to help Chef to find Whether he have suffiecient amount of money in his pocket for paying to ola.

if yes print “yes”(without quotes) else print “no”(without quotes)

EXPLANATION:
From the Question,its clear we have to find distance between two points.

Distance Formula: Given the two points (x1, y1) and (x2, y2), the distance d between these points is given by the:![alt text][3]

find the distance d between 2 points then we know ola cost N rupees per unit so,

total cost=d(distance) ***** N(Cost of Per Unit).

so if total cost<=R(Amount chef have in his pocket) then Print yes else print no

AUTHOR’S AND TESTER’S SOLUTIONS:

class chefandolacab
{

public static void main(String[] args)

{

    Scanner sc=new Scanner(System.in);

    int x1=sc.nextInt();

    int y1=sc.nextInt();

    int x2=sc.nextInt();

    int y2=sc.nextInt();

    int r=sc.nextInt();

    int n=sc.nextInt();
   
    double dist=Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));

    if(dist*r<=n)
    System.out.println("yes");

    else
        System.out.println("no");

}

}

Edit-Correction :slight_smile:
[1]: https://www.codechef.com/problems/EXPCODE2
[2]: https://www.codechef.com/users/vivek96
[3]: https://discuss.codechef.com/upfiles/dfm.png


#2

If I am not wrong,

 int dist=(int)Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));

Wont this lead to precision errors? Meaning, if distance is 1.9 km, we are truncating it to 1km and calculating cost. Depending on the rate then, that (0.9 km * rate) might be a problem.


#3

yes ,but its okay,u can take ciel value if no issue of precision,but in general we don’t need explicit typecasting…


#4

It wouldn’t had been okay had I been the problem setter :p.