Escolar Documentos
Profissional Documentos
Cultura Documentos
SINF0064 – Programação II
Prof. Dr. Alcides Xavier Benicasa
alcides@ufs.br
Roteiro
Recursividade
Definição
Função Recursiva
Condição de Parada
Efeitos da Recursão
Pilhas e Rotinas Recursivas
Recursividade
Quando e Como usar
Recursão vs. Iteração
2/49
1
Recursividade
Definição
4/49
2
Recursividade
5/49
Recursividade
2 passos:
• Ex.: S(1) = 1
6/49
3
Recursividade – Exemplo 01
#include <iostream> #include <locale.h> using std::cin;
using std::cout; using std::endl;
int main( ){
setlocale (LC_ALL,"portuguese");
int n;
cout << "Informe o valor de n:\n";
cin >> n;
cout << "A soma é " << soma(n) << endl;
return 0;
}
7/49
Recursividade – Exemplo 01
8/49
4
Recursividade – Exemplo 02
9/49
Recursividade – Exemplo 02
Na matemática, o fatorial de
um número natural n,
representado por n!, é o
produto de todos os inteiros
positivos menores ou iguais a
n.
10/49
5
Recursividade – Exemplo 02
Recursivo
int fatorial (int numero){
return numero==0?1:numero*fatorial(numero-1);
}
Iterativo
int fatorial (int numero){
int resultado = numero;
if (numero == 0) resultado++;
while (numero > 1) resultado *= --numero;
return resultado;
}
11/49
Recursividade – Exemplo 02
#include <iostream> #include <locale.h> using std::cin;
using std::cout; using std::endl;
int main( ){
setlocale (LC_ALL,"portuguese");
int n;
cout << "Informe o valor de n:\n";
cin >> n;
cout << "O fatorial é " << fatorial(n) << endl;
return 0;
}
12/49
6
Recursividade – Exercício para casa!!!
n! = (n+1)! / (n+1)
Alguns exemplos:
• 3! = (3 + 1)! / (3 + 1) = 4! / 4 = 24 / 4 = 6
• 2! = (2 + 1)! / (2 + 1) = 3! / 3 = 6 / 3 = 2
• 1! = (1 + 1)! / (1 + 1) = 2! / 2 = 2 / 2 = 1
• 0! = (0 + 1)! / (0 + 1) = 1! / 1 = 1 / 1 = 1
13/49
Efeitos da Recursão
7
Efeitos da Recursão
Pilhas e Rotinas Recursivas
15/49
int main( ){
cout << "O fatorial de 4 é " << fatorial(4) << endl;
return 0;
}
n ret n ret n ret n ret n ret
8
Pilhas e Rotinas Recursivas
//...
int fatorial (int n){
if (n==0)
return (1);
else
return (n * fatorial(n-1));
}
int main( ){
cout << "O fatorial de 4 é " << fatorial(4) << endl;
return 0;
}
n ret N ret n ret n ret n ret
4 4x?=?
int main( ){
cout << "O fatorial de 4 é " << fatorial(4) << endl;
return 0;
}
n ret n ret n ret n ret n ret
3 3x?=?
4 4x?=? 4 4x?=?
9
Pilhas e Rotinas Recursivas
//...
int fatorial (int n){
if (n==0)
return (1);
else
return (n * fatorial(n-1));
}
int main( ){
cout << "O fatorial de 4 é " << fatorial(4) << endl;
return 0;
}
n ret n ret n ret n ret n ret
2 2x?=?
3 3x?=? 3 3x?=?
int main( ){
cout << "O fatorial de 4 é " << fatorial(4) << endl;
return 0;
}
n ret n ret n ret n ret n ret
1 1x?=?
2 2x?=? 2 2x?=?
10
Pilhas e Rotinas Recursivas
//...
int fatorial (int n){
if (n==0)
return (1);
else
return (n * fatorial(n-1));
}
int main( ){
cout << "O fatorial de 4 é " << fatorial(4) << endl;
return 0;
}
n ret n ret n ret n ret n ret
0 1
1 1x?=? 1 1x?=?
int main( ){
cout << "O fatorial de 4 é " << fatorial(4) << endl;
return 0;
}
n ret n ret n ret n ret n ret
1 1x1=1
2 2x?=? 2 2x?=?
11
Pilhas e Rotinas Recursivas
//...
int fatorial (int n){
if (n==0)
return (1);
else
return (n * fatorial(n-1));
}
int main( ){
cout << "O fatorial de 4 é " << fatorial(4) << endl;
return 0;
}
n ret n ret n ret n ret n ret
2 2x1=2
3 3x?=? 3 3x?=?
int main( ){
cout << "O fatorial de 4 é " << fatorial(4) << endl;
return 0;
}
n ret n ret n ret n ret n ret
3 3x2=6
4 4x?=? 4 4x?=?
12
Pilhas e Rotinas Recursivas
//...
int fatorial (int n){
if (n==0)
return (1);
else
return (n * fatorial(n-1));
}
int main( ){
cout << "O fatorial de 4 é " << fatorial(4) << endl;
return 0;
}
n ret n ret n ret n ret n ret
4 4x6=24
int main( ){
cout << "O fatorial de 4 é " << fatorial(4) << endl;
return 0;
}
n ret n ret n ret n ret n ret
13
Efeitos da Recursão
Pilhas e Rotinas Recursivas
A cada chamada
Empilham-se na memória os dados locais (variáveis
e parâmetros) e o endereço de retorno
• A função corrente só termina quando a função chamada
terminar
27/49
Recursividade – Exemplo 03
Recursivo
28/49
14
Recursividade – Exemplo 03
#include <iostream>
#include <locale.h>
using std::cin;
using std::cout;
using std::endl;
int main( )
{
setlocale (LC_ALL,"portuguese");
int v[3] = {10,4,54};
imprime(v,3,0);
return 0;
}
29/49
int main( ) {
setlocale (LC_ALL,"portuguese");
Mesmo resultado, com
int v[3] = {10,4,54}; diferença de haver
imprime0(v,3,0);
return 0; }
duplicação de código
30/49
15
Efeitos da Recursão
Pilhas e Rotinas Recursivas
31/49
Recursividade
16
Recursão
Como usar
1º ponto: definir o problema de forma recursiva,
ou seja, em termos dele mesmo
2º ponto: definir a condição de término (ou
condição básica)
3º ponto: a cada chamada recursiva, deve-se
tentar garantir que se está mais próximo de
satisfazer a condição de término (caso mais
simples)
• Caso contrário, qual o problema?
33/49
Recursão
Problema do fatorial
34/49
17
Recursão
Quem é melhor?
return(fat); return(fat);
} }
35/49
Exemplo 05 - Fibonacci
18
Exemplo 05 - Fibonacci
37/49
38/49
19
39/49
40/49
20
41/49
21
Exemplo 05 - Fibonacci
Recursividade – Exemplo 05
#include <iostream> #include <locale.h> using std::cin;
using std::cout; using std::endl;
int fib(int n) {
int resultado;
if (n<2)
resultado=n;
else
resultado=fib(n-1)+fib(n-2);
return(resultado);
}
int main( )
{
setlocale (LC_ALL,"portuguese");
int n;
cout << "Informe o valor de n:\n";
cin >> n;
cout<< "O " << n <<"º número de Fibonacci é "<< fib(n) <<endl;
return 0;
} 44/49
22
Exemplo 05 - Fibonacci
Quem é melhor?
n 10 20 30 50 100
Recursão 8ms 1s 2min 21dias 109anos
45/49
46/49
23
Recursão vs. Iteração
int fatorial(int n) {
int fat;
if (n==0) fat=1;
else
fat=n*fatorial(n-1);
looping
return(fat);
}
47/49
48/49
24
Referências
49/49
25