38>growingPlant
Each day a plant is growing by upSpeed meters. Each night that plant's height decreases by downSpeed meters due to the lack of sun heat. Initially, plant is 0 meters tall. We plant the seed at the beginning of a day. We want to know when the height of the plant will reach a certain level. Example For upSpeed = 100, downSpeed = 10 and desiredHeight = 910, the output should be growingPlant(upSpeed, downSpeed, desiredHeight) = 10. |
code>>
int growingPlant(int upSpeed, int downSpeed, int desiredHeight) {
int dis = upSpeed;
if (dis >= desiredHeight) return 1;
return desiredHeight / (upSpeed - downSpeed);
}
int growingPlant(int upSpeed, int downSpeed, int desiredHeight) {
int grows = upSpeed;
int day = 1;
while (grows < desiredHeight)
{
grows -= downSpeed;
day++;
grows += upSpeed;
}
return day;
}