Answers for "primtiive calculator in c++"

C++
0

primtiive calculator in c++

#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>

long long f(long long n, vector <long long> arr)
{ 
    arr[1]=0;

    arr.push_back(n);
    long long ans=0, ret=0;

    if (n==1)
    {
        return (0);
    }
    ans= f(n-1, arr) + 1;


    if (n%2==0)
    {
        ret= f(n/2, arr) + 1;
        if (ret<ans)
        {
            ans=ret;
            std::cout<<ans<<'\n';
        }
    }
    if (n%3==0)
    { 
        ret= f(n/3, arr) + 1;
        if (ret<ans)
        {
            ans=ret;
            std::cout<<ans<<'\n';
        }
    }
    arr[n]=ans;

    return arr[n];    
}

int main() {

    long long n;
    std::cin >> n;
    std::vector<long long> arr;
    std::cout<<f(n, arr);

    return 0;
}
Posted by: Guest on September-18-2020

Browse Popular Code Answers by Language