描述
引自codewars:
All we eat is water and dry matter.
John bought potatoes: their weight is 100 kilograms. Potatoes contain water and dry matter.
The water content is 99 percent of the total weight. He thinks they are too wet and puts them in an oven - at low temperature - for them to lose some water.
At the output the water content is only 98%.
What is the total weight in kilograms (water content plus dry matter) coming out of the oven?
He finds 50 kilograms and he thinks he made a mistake: “So much weight lost for such a small change in water content!”
Can you help him?
Write function potatoes with
int parameter p0 - initial percent of water-
int parameter w0 - initial weight -
int parameter p1 - final percent of water -
potatoesshould return the final weight coming out of the oven w1 truncated as an int.
分析
这道题,逻辑思路并不难,只不过有一点容易理解错,也就是:水分含量变为98%,参照重量不再是先前的100kg,而是缩水后的包含水分与固体的总重量。
假设缩水后水分重量为x,则:
x / (x + w0 * (100 - p0) / 100) = p1 / 100
x = p1 * w0 * (100 - p0) / (100 * (100 - p1))
缩水后总重量为:
x / (p1 / 100) = w0 * (100 - p0) / (100 - p1)
实现
func Potatoes(p0, w0, p1 int) int {
return w0 * (100 - p0) / (100 - p1)
}