0

Some cases don't work in this cpp code

#include <iostream> using namespace std; int q(int n){ if((n==1)||(n==2)){ return 1; }else{ return (q(n-(q(n-1)))+q(n-(q(n-2)))); } } int main() { int n; cin>>n; cout<<q(n); return 0; }

2nd Aug 2024, 7:44 PM
Abbas Maatouk
3 Réponses
+ 1
Going down the memoization rabbit hole with Python, it is actually able to handle larger values of n than the C++ ones, primarilly because you cannot set the stack size for the C++ compiler in Sololearn... https://sololearn.com/compiler-playground/cBs1xhrEC4AA/?ref=app
4th Aug 2024, 4:45 AM
Bob_Li
Bob_Li - avatar
+ 4
As far as I know, the execution time limit in playground is 10 seconds. If your program need a time longer than that, it will time out and display "no output".
3rd Aug 2024, 12:50 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 2
use memoization to speed it up. There will still be a limit, but it will be higher. Your function calls easily go beyond millions for n>=11 without memoization. That is the penalty of recursive functions. Memoization reduces the function call count. https://sololearn.com/compiler-playground/cm3ECuHl1Fp8/?ref=app https://sololearn.com/compiler-playground/cin4yr31dCUY/?ref=app https://sololearn.com/compiler-playground/c0aDmyOTP87q/?ref=app
3rd Aug 2024, 3:41 PM
Bob_Li
Bob_Li - avatar