- Factorial
public class factorial {
private static inout io = new inout();
public static void main (String args[]) throws Exception
{
int a,m,p;a=io.readint();m=factorialrec(a);p=factorial2(a);io.writeln(m);io.writeln(p);
}
public static int factorialrec(int x)
{
int aux;aux=1;if (x==0)aux=1;else aux=aux*factorialrec(x-1);return aux;
}
public static int factorial2(int x)
{
int i,f;i=0;f=1;while (i != x)
{
i=i+1;f=f*i;}
return f;
}
}
- Fibonacci
public class Fibonacci {
public static long fib(int n)
{
if (n <= 1) return n;
else
return fib(n-1) + fib(n-2);
}
public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
for (int i = 1; i <= N; i++)
System.out.println(i + ": " + fib(i));
}
}
- Algoritmo Recursivo Torres de Honoi
Hanoi (dim N , palo A, palo B , palo C)
// N, origen, destino , auxiliar
Si N == 1
Imprimir : Pasar disco de A a B
else
Hanoi(N-1 , A , C, B)
Imprimir : Pasar disco de A a B
Hanoi(N-1 , C , B , A)
No hay comentarios:
Publicar un comentario