Problem Statement
You have deployed an ad hoc network to take temperature readings in a test area. When data from the network arrives back at the main control device it has the form "temp x y". temp is the temperature reading and x,y are the coordinates of the device sampling the temperature. Given two ints X and Y describing the location to measure, you must find which readings are most relevant. More precisely, given a String[] readings containing data of the form specified earlier, you will return the temperature which is closest to the location X,Y. Closeness is measured by the standard distance formula:
dist = sqrt( (x-X)^2 + (y-Y)^2 ).For convenience, no two readings in data will have the same distance from the point X,Y.
Definition
- Class:
- AdhocNetwork
- Method:
- bestTemp
- Parameters:
- String[], int, int
- Returns:
- int
- Method signature:
- int bestTemp(String[] data, int X, int Y)
- (be sure your method is public)
Constraints
- data will contain between 2 and 50 elements inclusive.
- Each element of data will have the form (quotes for clarity) "temp x y" where temp, x, and y are integers between 0 and 100 inclusive with no extra leading zeros.
- X will be between 0 and 100 inclusive.
- Y will be between 0 and 100 inclusive.
- No two readings in data will have the same distance from the point X,Y.
Examples
{"50 0 0","100 10 10"}0
0
Returns: 50
The first reading is at the same location as X,Y.{"1 1 1","2 2 2","3 3 3","4 4 4","5 5 5","6 6 6"}100
100
Returns: 6
The last reading is closest to 100,100.{"100 100 100","0 0 0"}51
51
Returns: 100
The first reading is just a bit closer.{"50 44 97","70 71 20","1 95 40","97 1 90","44 86 35","11 17 33","67 83 52","26 21 37", "63 25 8","45 21 45","27 26 93","14 3 83","79 95 72","100 2 17","69 22 98","80 45 63", "58 16 79","63 62 77","88 90 97","20 47 48","83 0 37","1 20 0","49 99 66","35 8 43", "18 40 96","20 16 38","99 48 77","14 28 87","2 41 73","68 74 74","61 83 82","92 96 12", "51 57 1","26 34 7","0 73 1","1 58 9","71 83 76","51 49 84","13 15 22","70 16 59", "52 22 49","33 0 79","89 42 89","39 47 99","84 22 7","19 1 36","48 94 96","16 43 32", "11 56 64","40 18 42"}87
20
Returns: 92
Random test case.