CarBuyer
SRM 347 · 2007-05-01 · by StevieT
SRM 347 · 2007-05-01 · by StevieT · Simple Math, Sorting
Problem Statement
Problem Statement
You wish to buy a new car and would like to get the cheapest option available. However, you recognize that the initial purchase cost is only a part of what you will need to spend on the car and you want to factor tax and fuel costs into your decision. You will be given a String[] cars giving the specifications of various models of car and an int annualDistance containing the distance that you will drive each year. For the car that you choose, the purchase cost only has to be paid once, the tax cost specific to the model has to be paid once per year and the fuel cost should be calculated as FUEL PRICE * DISTANCE DRIVEN / FUEL EFFICIENCY, where the fuel efficiency is specific to the car. You should calculate which car model in cars has the lowest cost on aggregate after years amount of time and return a double containing this cost.
Each element of cars will be formatted "<PURCHASE COST> <TAX> <FUEL EFFICIENCY>" (quotes for clarity).
Each element of cars will be formatted "<PURCHASE COST> <TAX> <FUEL EFFICIENCY>" (quotes for clarity).
Notes
- The return value must be accurate to within an absolute or relative tolerance of 1e-9.
Constraints
- cars will contain between 1 and 50 elements, inclusive.
- Each element of cars will contain between 5 and 15 characters, inclusive.
- Each element of cars will be formatted "<PURCHASE COST> <TAX> <FUEL EFFICIENCY>".
- Each <PURCHASE COST> will be an integer, without leading zeros, between 1 and 100000, inclusive.
- Each <TAX> will be an integer, without leading zeros, between 1 and 1000, inclusive.
- Each <FUEL EFFICIENCY> will be an integer, without leading zeros, between 1 and 100, inclusive.
- fuelPrice will be between 1 and 100, inclusive.
- annualDistance will be between 1 and 100000, inclusive.
- years will be between 1 and 100, inclusive.
Examples
0)
{"10000 50 50","12000 500 10","15000 100 65","20000 20 80","25000 10 90"}
2
5000
2
Returns: 10500.0
Here, fuel is cheap, you have a low annual mileage and you don't want to keep the car very long, so you go with the first model, which has the lowest purchase cost.
1)
{"10000 50 50","12000 500 10","15000 100 65","20000 20 80","25000 10 90"}
8
25000
10
Returns: 45200.0
This is the same set of cars, but this time fuel is far more expensive and you're going to keep the car for longer and drive it further. Now the more fuel-efficient fourth model is the cheapest.
2)
{"8426 774 19","29709 325 31","30783 853 68","20796 781 3"
,"27726 4 81","20788 369 69","17554 359 34","12039 502 24"
,"6264 230 69","14151 420 65","25115 528 70","22234 719 55"
,"2050 926 40","18618 714 29","173 358 57"}
33
8673
64
Returns: 254122.44444444444
3)
{"7303 611 14","13323 204 40","29837 100 26","26126 652 51","26002 494 9","22016 314 26","22574 28 43","8837 288 41","28694 854 56","23144 152 75","6834 170 36","25820 139 7","31822 184 41","3090 669 56","19349 103 87","8607 699 9","7761 668 53","3245 554 3","23467 610 26","24559 115 27","18268 499 97","27078 934 2","13966 77 28","6945 885 97","19041 650 17","12799 879 41","21834 143 97","23191 988 92","10191 742 30","1681 251 37","13895 410 88","7133 317 41","11427 488 19","25859 799 65","26230 607 46","27398 382 84","12462 395 87","9515 903 19","1982 670 24","17736 918 15","40 548 39","26470 175 59","168 315 6","8662 687 66","15477 210 80","28435 687 10","16713 245 39","15613 544 66","16438 694 38","9557 910 74"}
20
961
100
Returns: 51740.9540229885
4)
{"386 167 17","21139 157 88","16392 864 28","23569 687 20","11322 799 29","13766 836 46","18691 217 38","3056 475 35","1513 253 5","23787 635 97","400 270 30","3472 223 54","20320 864 50","21766 405 86","24104 809 43","31325 279 6","15313 986 78","11605 687 47","5747 908 51","28952 453 25","5493 889 58","27351 400 25","1407 49 78","16259 335 10","26720 194 70","21276 96 69","22078 418 13","334 358 40","20387 704 14","6735 749 52","14405 499 1","24640 432 41","16547 369 78","16512 721 97","6339 239 53","25001 417 100","28856 561 73","10806 240 18","7103 867 73","27911 126 23","12553 562 58","16436 233 18","20885 197 97","30710 506 77","23718 187 31","8842 691 18","25778 209 18","860 372 60","13542 919 95","15071 962 50"}
11
5012
47
Returns: 36930.5641025641
Submissions are judged against all 41 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Coding Area
Language: C++17 · define a public class CarBuyer with a public method double lowestCost(vector<string> cars, int fuelPrice, int annualDistance, int years) · 41 test cases · 2 s / 256 MB per case