쉬움 0명 해결 정답률 0%

Driving Costs

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

배우게 될 내용

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

문제

번역본 보기

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.

제약 조건

0 < miles_per_gallon ≤ 200

0 ≤ dollars_per_gallon ≤ 20

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

예시

예시 1
입력:
miles_per_gallon = 25.0
dollars_per_gallon = 3.2
trips = [10.0, 50.0, 400.0]

예상 출력:
expected = [1.2800000000000002, 6.4, 51.2]
입력:
double miles_per_gallon = 25.0;
double dollars_per_gallon = 3.2;
std::vector<double> trips = {10.0, 50.0, 400.0};

예상 출력:
std::vector<double> expected = {1.2800000000000002, 6.4, 51.2};
입력:
double miles_per_gallon = 25.0;
double dollars_per_gallon = 3.2;
double[] trips = new double[] {10.0, 50.0, 400.0};

예상 출력:
double[] expected = new double[] {1.2800000000000002, 6.4, 51.2};

설명

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.

예시 2
입력:
miles_per_gallon = 40.0
dollars_per_gallon = 4.0
trips = [0.0, 20.0]

예상 출력:
expected = [0.0, 2.0]
입력:
double miles_per_gallon = 40.0;
double dollars_per_gallon = 4.0;
std::vector<double> trips = {0.0, 20.0};

예상 출력:
std::vector<double> expected = {0.0, 2.0};
입력:
double miles_per_gallon = 40.0;
double dollars_per_gallon = 4.0;
double[] trips = new double[] {0.0, 20.0};

예상 출력:
double[] expected = new double[] {0.0, 2.0};

설명

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

접근 방법

먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.

힌트 1

Gallons used is the distance divided by miles per gallon.

힌트 2

Multiply the gallons by the price per gallon.

힌트 3

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

예상 복잡도

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

참고 자료

이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

코더마다 가장 좋은 통과 풀이입니다. 더 빠르고, 메모리를 덜 쓰고, 평가가 좋은 풀이가 위에 옵니다.

아직 통과한 풀이가 없습니다. 풀어서 1위를 차지하세요.

다른 사람의 풀이 보기

커뮤니티 솔루션

아직 공개된 풀이가 없습니다. 문제를 풀고 첫 풀이를 공유하세요.