모든 챌린지
쉬움 0명 해결 정답률 0%

Caffeine Half-Life

Find how much caffeine is left in the body after a number of hours, given its half-life.

배우게 될 내용

  • Apply exponential decay with a half-life
  • Use the power function of your language
  • Avoid integer division in a fraction of half-lives

문제

번역본 보기

Caffeine leaves the body gradually: after one half-life only half of it is left, after two half-lives a quarter, and so on. The amount left after hours hours is milligrams × 0.5^(hours / half_life_hours), and the formula works for any number of hours, not only whole half-lives.

Implement caffeine_half_life(milligrams, half_life_hours, hours).

Input

milligrams: The caffeine taken at the start, in milligrams.

half_life_hours: The half-life in hours (greater than 0).

hours: How many hours have passed.

Output

Return the milligrams left after hours hours. Do not round; answers are checked to within 0.000001.

제약 조건

0 ≤ milligrams ≤ 2000

0 < half_life_hours ≤ 24

0 ≤ hours ≤ 240

예시

예시 1
입력:
milligrams = 160.0
half_life_hours = 6.0
hours = 12.0

예상 출력:
expected = 40.0
입력:
double milligrams = 160.0;
double half_life_hours = 6.0;
double hours = 12.0;

예상 출력:
double expected = 40.0;
입력:
double milligrams = 160.0;
double half_life_hours = 6.0;
double hours = 12.0;

예상 출력:
double expected = 40.0;

설명

12 hours is two half-lives, so 160 becomes 80 and then 40.

예시 2
입력:
milligrams = 100.0
half_life_hours = 5.0
hours = 0.0

예상 출력:
expected = 100.0
입력:
double milligrams = 100.0;
double half_life_hours = 5.0;
double hours = 0.0;

예상 출력:
double expected = 100.0;
입력:
double milligrams = 100.0;
double half_life_hours = 5.0;
double hours = 0.0;

예상 출력:
double expected = 100.0;

설명

No time has passed, so all 100 mg are still there.

접근 방법

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

힌트 1

First work out how many half-lives have passed: hours divided by the half-life.

힌트 2

Each half-life multiplies the amount by 0.5.

힌트 3

Make sure the division is done with decimals, not whole numbers.

예상 복잡도

O(1) time, O(1) extra space

참고 자료

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

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

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

다른 사람의 풀이 보기

커뮤니티 솔루션

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