👤

The snail crawls up the column. During the day it crawls up some distance. During the night she sleeps, so she slides down for some distance (less than crawls up during the day).


Cum se rezolva asta ??


Your function takes three arguments:


The height of the column (meters)

The distance that the snail crawls during the day (meters)

The distance that the snail slides down during the night (meters)

Calculate number of day when the snail will reach the top of the column.


Am incercat

function snail(column, day, night){

let netDistance=day-night

let days= column/netDistance

console.log(days)

return days

}


si pica 2 teste ...



Răspuns :

Răspuns:

#include <iostream>

using namespace std;

int snail(int column, int day, int night)

{

   int d=0, h=0, dif=day-night;

   while (h+day<column)

   {

       ++d;

       h=h+dif;

   }

   return d+1;

}

int main()

{

   int inaltimea, ziua, noapte;

   cout << "inaltimea stalpului = "; cin >> inaltimea;

   cout << "cati metri urca ziua? "; cin >> ziua;

   cout << "cati metri coboara noaptea? "; cin >> noapte;

   cout << "melcul ajunge in varful stalpului in ziua a ";

   cout << snail(inaltimea, ziua, noapte) << "-a";

}

Explicație: