combinatorios
This commit is contained in:
1 parent
be1108d422
commit
65d6f20802
3 files changed
+98
-2
No files matched your search
@@ -2,7 +2,6 @@ import java.util.*;
|
|||||||
|
|
||||||
public class Metodos3 {
|
public class Metodos3 {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Scanner sc = new Scanner(System.in);
|
|
||||||
boolean detener = false;
|
boolean detener = false;
|
||||||
int num = 0;
|
int num = 0;
|
||||||
while(!detener){
|
while(!detener){
|
||||||
@@ -13,7 +12,6 @@ public class Metodos3 {
|
|||||||
} else detener = true;
|
} else detener = true;
|
||||||
}
|
}
|
||||||
System.out.printf("El factorial de %d es %.2f\n", num, factorial(num));
|
System.out.printf("El factorial de %d es %.2f\n", num, factorial(num));
|
||||||
sc.close();
|
|
||||||
}
|
}
|
||||||
private static double factorial(int n) {
|
private static double factorial(int n) {
|
||||||
double result = 1.00;
|
double result = 1.00;
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user