In the above function, I create a static array. How can it be reset once when entering the function for the first time?
typedef unsigned char uchar; typedef unsigned long long int ullong; // Π Π΅ΠΊΡΡΠ΅Π½ΡΠ½ΡΠΉ Π°Π»Π³ΠΎΡΠΈΡΠΌ Π½Π°Ρ
ΠΎΠΆΠ΄Π΅Π½ΠΈΡ F(n) // F(n) - ΡΠΈΡΠ»ΠΎ Π€ΠΈΠ±ΠΎΠ½Π°ΡΠΈ, n - ΠΏΠΎΡΡΠ΄ΠΊΠΎΠ²ΡΠΉ Π½ΠΎΠΌΠ΅Ρ ullong Fibonachi_Recursion_With_Memory(const uchar &_n) { static ullong *const M = new ullong[_n + 1]; // ΠΠΈΠ½Π°ΠΌΠΈΡΠ΅ΡΠΊΠΈ Π²ΡΠ΄Π΅Π»Π΅Π½ΡΠΉ ΠΌΠ°ΡΡΠΈΠ² (ΠΊΠΎΡΠΎΡΡΠΉ Π½ΡΠΆΠ½ΠΎ ΠΎΠ±Π½ΡΠ»ΠΈΡΡ) ////////////////////////////////////////////////////////////////////// - (ΠΌΠΎΠΉ ΡΠΏΠΎΡΠΎΠ±) static bool emptyVector(false); if(!emptyVector) { for(uchar i = 0; i <= _n; i++) M[i] = 0; emptyVector = true; } ////////////////////////////////////////////////////////////////////// if(_n <= 2) return 1; if(M[_n] != 0) return M[_n]; return M[_n] = Fibonachi_Recursion_With_Memory(_n - 1) + Fibonachi_Recursion_With_Memory(_n - 2); }
Fibonachi_Recursion_With_Memory(1); Fibonachi_Recursion_With_Memory(5);
Fibonachi_Recursion_With_Memory(1); Fibonachi_Recursion_With_Memory(5);
it will be nonsense, because the array in the second case will be 1 element in size. - zenden2k