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; }
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
+ 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".
+ 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