From 65d6f20802db42e3e64f70baa0deaa37a4017e08 Mon Sep 17 00:00:00 2001 From: PC de clase Date: Thu, 29 Oct 2026 12:13:34 +0100 Subject: [PATCH] combinatorios --- src/Metodos3.java | 2 -- src/Metodos4.java | 48 +++++++++++++++++++++++++++++++++++++++++++++ src/Metodos5.java | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 src/Metodos4.java create mode 100644 src/Metodos5.java diff --git a/src/Metodos3.java b/src/Metodos3.java index 3291731..11d43a0 100644 --- a/src/Metodos3.java +++ b/src/Metodos3.java @@ -2,7 +2,6 @@ import java.util.*; public class Metodos3 { public static void main(String[] args) { - Scanner sc = new Scanner(System.in); boolean detener = false; int num = 0; while(!detener){ @@ -13,7 +12,6 @@ public class Metodos3 { } else detener = true; } System.out.printf("El factorial de %d es %.2f\n", num, factorial(num)); - sc.close(); } private static double factorial(int n) { double result = 1.00; diff --git a/src/Metodos4.java b/src/Metodos4.java new file mode 100644 index 0000000..2f90f31 --- /dev/null +++ b/src/Metodos4.java @@ -0,0 +1,48 @@ +import java.util.*; + +public class Metodos4 { + public static void main(String[] args) { + int n = leerEnteroPositivo(); + int m = leerEnteroPositivo(); + System.out.printf("El combinatorio de %d y %d es ",n, m ); + combinatorio(n, m); + System.out.println(""); + } + public static int factorial(int n) { + int result = 1; + while(n > 0) result *= n--; + return result; + } + public static void combinatorio(int n, int m){ + if (n < 0 ||m < 0) { + System.out.println("Los numeros deben ser positivos"); + return; + } + int numerador = factorial(n); + int denominador = factorial(m) * factorial(n - m); + System.out.printf("%d\t", numerador / denominador); + } + public static int leerEnteroPositivo() { + Scanner sc = new Scanner(System.in); + boolean parar = false; + int n = 0; + while(!parar) { + System.out.println("Introduce un numero: "); + try { + n = sc.nextInt(); + if (n > 0) { + return n; + } else { + System.out.println( + "Tiene que ser un número positivo." + ); + } + } catch(Exception e){ + System.out.println("Número invalido, intentalo otra vez"); + sc.next(); + } + } + sc.close(); + return n; + } +} diff --git a/src/Metodos5.java b/src/Metodos5.java new file mode 100644 index 0000000..9772ce7 --- /dev/null +++ b/src/Metodos5.java @@ -0,0 +1,50 @@ +import java.util.Scanner; + +public class Metodos5 { + public static void main(String[] args) { + int n = leerEnteroPositivo(); + for(int i = 0; i < n; i++){ + for(int j = 0; j <= i; j++){ + combinatorio(i, j); + } + System.out.println(); + } + } + public static int factorial(int n) { + int result = 1; + while(n > 0) result *= n--; + return result; + } + public static void combinatorio(int n, int m){ + if (n < 0 ||m < 0) { + System.out.println("Los numeros deben ser positivos"); + return; + } + int numerador = factorial(n); + int denominador = factorial(m) * factorial(n - m); + System.out.printf("%d\t", numerador / denominador); + } + public static int leerEnteroPositivo() { + Scanner sc = new Scanner(System.in); + boolean parar = false; + int n = 0; + while(!parar) { + System.out.println("Introduce un numero: "); + try { + n = sc.nextInt(); + if (n > 0) { + return n; + } else { + System.out.println( + "Tiene que ser un número positivo." + ); + } + } catch(Exception e){ + System.out.println("Número invalido, intentalo otra vez"); + sc.next(); + } + } + sc.close(); + return n; + } +}