telele97 Posted January 4, 2013 at 07:58 PM Report #489597 Posted January 4, 2013 at 07:58 PM Preciso de criar uma função recursiva que faça o seguinte cálculo : f(p, q) = pf(p − 2, q) + qf(p, q − 2) com f(0,q)=0, f(p,0)=1 e f(0,0)=5 sendo que p e 1 nao podem ser negativos comecei por fazer o seguinte código só que não consigo passar daqui :S int f(int p, int q){ int soma=0; if(p>=0 && q>=0){ if(p==0 && q!=0) return 0; else if(q==0 && p!=0) return 1; else if(q==0 && p==0) return 5; }else{ return -1; } return q*f(p-2,q); }
KTachyon Posted January 4, 2013 at 08:32 PM Report #489606 Posted January 4, 2013 at 08:32 PM (edited) A função só é recursiva se se chamar a si própria. No teu caso, a própria função nunca chega a ser chamada: int f(int p, int q) { int soma=0; if(p >= 0 && q >= 0) { if(p == 0 && q != 0) return 0; else if(q == 0 && p!=0) return 1; else if(q == 0 && p == 0) return 5; } else { return -1; } return q*f(p-2,q); } EDIT: Pois, faltou-me o caso em que os dois são diferentes de zero e a função passa. Tens que retornar o resultado das chamadas, ou seja return q*f(p-2, q) + p*f(p, q-2); Edited January 4, 2013 at 08:36 PM by KTachyon “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.” -- Tony Hoare
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now