Hi all,
This may be a naive questions.But I am unable to figure it out how recursive flow is being happening in the solution to this problem.Recursion is my weak point and I am unable to understand how things jump from one piece of code to another in recursion. I understand the basic level recursion where you break when certain condition meets.but herein i am not able to understand.
Is there anybody who is proficient in recursion ,please help!
Question: https://www.codechef.com/problems/COINS
Solution: (Skip the unnecessary printf statements in between the funct.)
#include <stdio.h>
long long funct(long long);
long long memory[10000000]={0};
int main() {
long long n=0, m=0;
while(scanf("%lld", &n)!=EOF) {
printf("%lld\n", funct(n));
}
return 0;
}
long long funct(long long n) {
if(n<12)
return n;
if(n==12)
return 13;
if(n<1000000) {
if(memory[n]!=0){
return memory[n];printf("here inside it");}
}
printf("n is :%d\n",n);
long long two, three, four;
two=n/2;
three=n/3;
four=n/4;
long long k =funct(two)+funct(three)+funct(four);
printf("k is %d\n",k);
// printf("funct(two) is: %lld\n",funct(two));
if(n<1000000){printf("inside if\n");
memory[n]=k;}
printf("one end\n");
//printf("k here %lld\n :",k)
return k;
}