Movement

An elephant decided to visit his friend. It turned out that the elephant's house is located at point 0 and his friend's house is located at point 

x(x>0) of the coordinate line. In one step the elephant can move 1, 2, 3, 4, or 5 positions forward.
Determine, what is the minimum number of steps he needs to make in order to get to his friend's house.

Example:

Input:  n = 26
Outputa: 6

Approach

C++

#include <bits/stdc++.h>
using namespace std;

long long movement(long long n)
{
    long long cnt = 0;
    while (n >= 5)
    {
        cnt++;
        n = n - 5;
    }
    while (n >= 4)
    {
        cnt++;
        n = n - 4;
    }
    while (n >= 3)
    {
        cnt++;
        n = n - 3;
    }
    while (n >= 2)
    {
        cnt++;
        n = n - 2;
    }
    if (n == 1)
        cnt++;
    return cnt;
}
int main()
{
    long long n = 26;

    cout << movement(n<< "\n";

    return 0;
}


No comments:

Post a Comment