Which Truck Is Closer?


TCEA State Programming Contest April 28, 2001

Problem 5.1 Which Truck Is Closer?

General Statement: The Jones Trucking Company tracks the location of each of its trucks on a grid similar to an (x, y) plane. The home office is at location (0, 0). Read the coordinates of truck A and the coordinates of truck B and determine which is closer to the office.

Input: The first line of the data set for this problem is an integer representing the number of collections of data that follow. Each collection contains 4 integers: the x-coordinate and then the y-coordinate of truck A followed by the x-coordinate and then the y-coordinate of truck B.

Output: All letters are upper case.

The output is to be formatted exactly like that for the sample output given below.

Assumptions: The x-coordinate is in the range –20 .. 20. The y-coordinate is in the range –20 .. 20.

Discussion: The distance between point #1 with coordinates (x1, y1) and point #2 with coordinates (x2, y2) is:

Sample Input:
4
3 -2 -5 -3
0 6 1 2
-7 8 4 -1
3 3 -2 2

Sample Output:
A IS CLOSER
B IS CLOSER
B IS CLOSER
B IS CLOSER

Solutions:


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

int main ()
{
    int dataSet;
    scanf ("%d", &dataSet);

    while ( dataSet-- ) {

        double xA, yA;
        scanf ("%lf %lf", &xA, &yA);

        double xB, yB;
        scanf ("%lf %lf", &xB, &yB);

        double distance_A = sqrt ( xA * xA + yA * yA );
        double distance_B = sqrt ( xB * xB + yB * yB );

        if ( distance_A > distance_B )
            printf ("B IS CLOSER\n");

        else
            printf ("A IS CLOSER\n");
    }

    return 0;
}


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s