ง่าย 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 กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

คำตอบที่ผ่านดีที่สุดของแต่ละคน คำตอบที่เร็วกว่า ใช้หน่วยความจำน้อยกว่า และได้คะแนนดีกว่าจะอยู่ด้านบน

ยังไม่มีคำตอบที่ผ่าน แก้โจทย์นี้แล้วคว้าอันดับหนึ่งไปเลย

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก