Code Wars — Deodorant Evaporator

stream’s blog
1 min readJun 11, 2019

--

This program tests the life of an evaporator containing a gas.

We know the content of the evaporator (content in ml), the percentage of foam or gas lost every day (evap_per_day) and the threshold (threshold) in percentage beyond which the evaporator is no longer useful. All numbers are strictly positive.

The program reports the nth day (as an integer) on which the evaporator will be out of use.

Note : Content is in fact not necessary in the body of the function “evaporator”, you can use it or not use it, as you wish. Some people might prefer to reason with content, some other with percentages only. It’s up to you but you must keep it as a parameter because the tests have it as an argument.

基本上就是寫出一個方法,測出容量可以用幾天

看解答蠻驚訝的,不用知道容量,只要知道蒸發量占幾%即可

迴圈的概念搭上計次,無法確定多少次,用while

public static int evaporator(double content, double evap_per_day, double threshold) {int result = 0;double percentage = 100;while (percentage > threshold) {percentage -= percentage * (evap_per_day / 100);result++;}return result;}

--

--