|
|
|
|
Bonsoir;
mystere est une fonction recursive, esque je peux l'ecrire la fonction mystere d'une façon non recurcive?
#include<stdio.h>
long int mystere(int n)
{
if(n==0)
return 2;
else
if(n==1)
return 1;
else
return (2*mystere(n-1)+4*mystere(n-2));
}
main()
{
int n;
do
{
printf("\n Introduire un nombre : ");
scanf("%d",&n);
}
while(n<0);
printf("\n mystere de %d = %ld ",n,mystere(n));
}
Configuration: Windows XP Firefox 2.0.0.1
C'est bon a deux trois détails près
#include <stdio.h>
long int mystere(unsigned int n)
{
if (n==0) return 2;
else if (n==1) return 1;
return (2*mystere(n-1)+4*mystere(n-2));
}
int main(){
int n;
do{
printf("\nIntroduire un nombre : ");
scanf("%d",&n);
}while(n<0);
printf("mystere de %d = %ld\n",n,mystere((unsigned int)n));
return 0;
}
En particulier la fonction main doit retourner 0 quand tout va bien (par convention), et on peut préciser un peu les types (utiliser unsigned int autant que posible) Bonne chance |
Répondre à mamiemando
|
Salut.
|