Jak się zabrać za: Napisz funkcję rekurencyjną zwracająca wartość funkcji danej wzorem
f1 = f2 = f3 =3;
fn =2fn-1 + fn/2 dla n>3 ?
0
0
Przeczytać rozdział o rekurencji z dowolnego kursu C++. Istnieje duża szansa, że taki program będzie wręcz przykładem. Tutaj nie ma nic z algorytmiki.
0
tylko ze tu nie trzeba rekurencji, albo cos zle przepisales.
Wiec domyslajac sie ze chodzilo CI o cos takiego
int Foo(int fn)
{
if (fn == 1 || fn == 2 || fn == 3)
{
return 3;
}
return Foo(2 * fn - 1 + fn / 2)
}
masz zrobione na intach, na floatach/double mozesz zrobic sobie sam.
0
Nie @fasadin fn =2fn-1 + fn/2
sugeruje bardziej f(n) = 2 * f(n-1) + f(n/2)
.
0
#include<iostream>
template<unsigned n, unsigned t=3>
struct f
{
template<unsigned n>
struct fgt
{
enum { value = 2 * f<n - 1>::VERSION::value + f<n / 2>::VERSION::value };
};
template<unsigned n>
struct fle
{
enum { value = f<n - 1>::VERSION::value };
};
template<>
struct fle<1>
{
enum { value = 3 };
};
template<unsigned n, bool condition>
struct selector
{
typedef fgt<n> SELECT;
};
template<unsigned n>
struct selector<n, false>
{
typedef fle<n> SELECT;
};
typedef typename selector<n,(n>t)>::SELECT VERSION;
};
int main()
{
std::cout << f<8>::VERSION::value << std::endl;
return 0;
}