COINS: Getting rte

#include
#include
#include
using namespace std;

int max(int a,int b)
{
    if(a>b) return a;
    else return b;
}

int f(int n,int val[])
{
    int a,b,c,d;
    
    if(n==0){val[n]=0; return val[n];}
    else if(n==1){val[n]=1; return val[n];}
    else if(n==2){val[n]=2; return val[n];}
  else if(val[n]!=0) {return val[n];}
  else  {b=f(n/2,val);
         c=f(n/3,val);
         d=f(n/4,val);
         a= max(n,b+c+d);
         val[n]=a;
         return a;}
         }

main()
{
      int n,ab;
      int val[10000]={0};
      while(cin>>n) {ab=f(n,val);cout<<ab<<endl;}
     // cout<<f(12)<<endl;
      
      }

Problem??

Please format your code properly and post.

Yes problem, your code is UNREADABLE

Please look at the limits :
0 <= n <= 1 000 000 000

Your solution is made for 0 <=n <= 10 000

Memory will overflow giving SIGSEGV

1 Like