알고리즘/codefights

38>growingPlant

Diademata 2017. 8. 7. 23:44
반응형

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;

}

반응형