Dễ 0 đã giải Tỷ lệ chấp nhận 0%

Driving Costs

Work out the fuel cost of several trips from a car's miles per gallon and the gas price.

Bạn sẽ học được gì

  • Convert miles to gallons using miles per gallon
  • Turn gallons into dollars
  • Build a result list in the same order as the input

Đề bài

Xem bản dịch

Before a road trip, Maya wants to know what each leg will cost in gas. Her car goes miles_per_gallon miles on one gallon, and gas costs dollars_per_gallon dollars a gallon. A trip of m miles uses m / miles_per_gallon gallons, and each gallon costs dollars_per_gallon.

Implement driving_costs(miles_per_gallon, dollars_per_gallon, trips).

Input

miles_per_gallon: How many miles the car drives on one gallon (greater than 0).

dollars_per_gallon: The price of one gallon in dollars.

trips: The length of each trip in miles.

Output

Return the cost in dollars of each trip, in the same order as trips. Do not round; answers are checked to within 0.000001. An empty list of trips gives an empty list.

Ràng buộc

0 < miles_per_gallon ≤ 200

0 ≤ dollars_per_gallon ≤ 20

0 ≤ length of trips ≤ 100, each trip between 0 and 10,000 miles

Ví dụ

Ví dụ 1
Đầu vào:
miles_per_gallon = 25.0
dollars_per_gallon = 3.2
trips = [10.0, 50.0, 400.0]

Kết quả mong đợi:
expected = [1.2800000000000002, 6.4, 51.2]
Đầu vào:
double miles_per_gallon = 25.0;
double dollars_per_gallon = 3.2;
std::vector<double> trips = {10.0, 50.0, 400.0};

Kết quả mong đợi:
std::vector<double> expected = {1.2800000000000002, 6.4, 51.2};
Đầu vào:
double miles_per_gallon = 25.0;
double dollars_per_gallon = 3.2;
double[] trips = new double[] {10.0, 50.0, 400.0};

Kết quả mong đợi:
double[] expected = new double[] {1.2800000000000002, 6.4, 51.2};

Giải thích

10 miles uses 0.4 gallons, which costs 0.4 × 3.2 = 1.28 dollars; 50 miles costs 6.4 and 400 miles costs 51.2.

Ví dụ 2
Đầu vào:
miles_per_gallon = 40.0
dollars_per_gallon = 4.0
trips = [0.0, 20.0]

Kết quả mong đợi:
expected = [0.0, 2.0]
Đầu vào:
double miles_per_gallon = 40.0;
double dollars_per_gallon = 4.0;
std::vector<double> trips = {0.0, 20.0};

Kết quả mong đợi:
std::vector<double> expected = {0.0, 2.0};
Đầu vào:
double miles_per_gallon = 40.0;
double dollars_per_gallon = 4.0;
double[] trips = new double[] {0.0, 20.0};

Kết quả mong đợi:
double[] expected = new double[] {0.0, 2.0};

Giải thích

A 0 mile trip costs nothing, and 20 miles uses half a gallon, so [0.0, 2.0].

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

Gallons used is the distance divided by miles per gallon.

Gợi ý 2

Multiply the gallons by the price per gallon.

Gợi ý 3

Do the same calculation for every trip and keep the order.

Độ phức tạp mong đợi

O(n) time, O(n) extra space

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Lời giải đạt tốt nhất của mỗi người. Lời giải nhanh hơn, tốn ít bộ nhớ hơn và được đánh giá cao hơn sẽ đứng trên.

Chưa có lời giải nào đạt. Giải bài này để giành hạng nhất.

Học hỏi từ người khác

Giải pháp cộng đồng

Chưa có giải pháp nào được chia sẻ. Hãy giải thử thách này để đăng giải pháp đầu tiên.