Laboratorio de Informática 111
Laboratorio de Informática 111
Laboratorio de Informática 111
CARRERA DE INFORMÁTICA
GESTIÓN 2/22
La Paz – Bolivia
2/22
1
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
Secuenciales - Condicionales
Entrada
5
Salida
50
Python Java
R = int(input("Ingrese radio de la circunf.:")) import java.util.Scanner;
AreaC = 2*(R**2) public class ejer1 {
print("El area es: ",AreaC) public static void main(String[] args){
int R,AreaC;
Scanner sc = new Scanner(System.in);
System.out.print("Ingrese el radio de la circunf: ");
R = sc.nextInt();
AreaC = 2 * ((int)Math.pow(R, 2));
System.out.println(AreaC);
}
}
2
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
Python Java
Altura = int(input("Ingrese la altura: ")) import java.util.Scanner;
Largo = int(input("Ingrese el largo: ")) public class ejer2 {
Ancho = int(input("Ingrese el ancho: ")) public static void main(String[] args){
areaTotal = 2 * Altura * (Largo + Ancho) int Altura,Largo,Ancho,areaTotal,precioTotal;
precioTotal = areaTotal * 6 Scanner sc = new Scanner(System.in);
print(areaTotal,precioTotal) System.out.print("Ingrese la altura: ");
Altura = sc.nextInt();
System.out.print("Ingrese la largo: ");
Largo = sc.nextInt();
System.out.print("Ingrese la ancho: ");
Ancho = sc.nextInt();
areaTotal = 2 * Altura * (Largo + Ancho);
precioTotal = areaTotal * 6;
System.out.println(areaTotal+" "+precioTotal);
}
}
Corrida Python Corrida Java
Entrada
35, 116
Salida
23, 12
Python Java
nroCabezas = int(input("Ingrese el nro de cabezas: ")) import java.util.Scanner;
nroPatas = int(input("Ingrese el nro de patas: ")) public class ejer3 {
cantOve = (nroPatas - 2*nroCabezas) // 2 public static void main(String[] args){
cantPato = nroCabezas - cantOve int nroCabezas,nroPatas,cantOve,cantPato;
print(cantOve,cantPato) Scanner sc = new Scanner(System.in);
System.out.print("Ingrese el nro de cabezas: ");
nroCabezas = sc.nextInt();
System.out.print("Ingrese el nro de patas: ");
nroPatas = sc.nextInt();
cantOve = (nroPatas - (2 * nroCabezas)) / 2;
cantPato = nroCabezas - cantOve;
System.out.println(cantOve+" "+cantPato);
}
}
3
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
Entrada
123
Salida
4, 3
Python Java
import math import java.util.Scanner;
nro = int(input("Ingrese un nro: ")) public class ejer4 {
digDer = nro % 10 public static void main(String[] args){
cantDig = int(math.log(nro,10)) + 1 int nro,digDer,cantDig,digIzq,suma,producto;
digIzq = nro // (10 ** (cantDig-1)) Scanner sc = new Scanner(System.in);
suma = digDer + digIzq System.out.print("Ingrese un nro: ");
producto = digDer * digIzq nro = sc.nextInt();
print(suma,producto) digDer = nro % 10;
cantDig = (int) Math.log10(nro) + 1;
digIzq = nro / (int) Math.pow(10, cantDig-1);
suma = digDer + digIzq;
producto = digDer * digIzq;
System.out.println(suma+" "+producto);
}
}
Corrida Python Corrida Java
Entrada
25, 3
Salida
375
4
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
Python Java
vel = int(input("Ingrese la velocidad: ")) import java.util.Scanner;
tiempo = int(input("Ingrese el tiempo registrado: ")) public class ejer5 {
distancia = vel * tiempo public static void main(String[] args){
precioTotal = distancia * 5 int vel,tiempo,distancia,precioTotal;
print(precioTotal) Scanner sc = new Scanner(System.in);
System.out.print("Ingrese la velocidad: ");
vel = sc.nextInt();
System.out.print("Ingrese el tiempo: ");
tiempo = sc.nextInt();
distancia = vel * tiempo;
precioTotal = distancia * 5;
System.out.println(precioTotal);
}
}
Corrida Python Corrida Java
Condicionales
Entrada
20, 100, 20, 20, 20
Salida
171
Python Java
p1 = int(input()) import java.util.Scanner;
p2 = int(input()) public class ej1 {
p3 = int(input()) public static void main(String[] args) {
p4 = int(input()) Scanner lee = new Scanner(System.in);
p5 = int(input()) double p1 = lee.nextDouble();
CT = p1+p2+p3+p4+p5 double p2 = lee.nextDouble();
if CT>200: double p3 = lee.nextDouble();
DCT=CT*0.25 double p4 = lee.nextDouble();
CT=CT-DCT double p5 = lee.nextDouble();
print(CT) double CT = p1+p2+p3+p4+p5;
else: if (CT>200) {
DCT=CT*0.05 double DCT = CT*0.25;
CT=CT-DCT CT = CT-DCT;
print(CT) System.out.println(CT);
}else {
double DCT = CT*0.05;
CT = CT-DCT;
System.out.println(CT);
}
} }
5
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
Entrada
0, 0
Salida
3
Entrada
35, 40
Salida
1
Python Java
x = int(input()) import java.util.*;
y = int(input()) public class ej2 {
dis = (x*2+y*2)*0.5 public static void main(String[] args) {
a=0 Scanner lee = new Scanner(System.in);
b=1 double x = lee.nextDouble();
c=2 double y = lee.nextDouble();
double dis = (x*2+y*2)*0.5;
6
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
Entrada
5
Salida
0
Entrada
18
Salida
3
Python Java
edad = int(input()) import java.util.Scanner;
a=0 public class ej3 {
b=1 public static void main(String[] args) {
c=2 Scanner lee = new Scanner(System.in);
d=3 int edad=lee.nextInt();
if edad>=60: int a=0,b=1,c=2,d=3;
print(b) if (edad>=60) {
elif edad>=18: System.out.println(b);
print(d) }else if (edad>=18) {
elif edad>5: System.out.println(d);
print(c) }else if (edad>5) {
else: System.out.println(c);
print(a) }else {
System.out.println(a);
}
}
7
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
}
Corrida Python Corrida Java
8
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
Entrada
‘*’, 3, 3
Salida
9
Entrada
‘/’, 9, 9
Salida
1
Python Java
op = input() import java.util.Scanner;
a = int(input()) public class ej5 {
b = int(input()) public static void main(String[] args) {
if op == “+”: Scanner lee = new Scanner(System.in);
R=a+b String op = lee.nextLine();
print(R) double a=lee.nextInt();
elif op == “-”: double b=lee.nextInt();
R=a-b double R;
print(R) switch (op) {
elif op == “*”: case "+":
R=a*b R=a+b;
print(R) System.out.println(R);
elif op == “/”: break;
R=a/b case "-":
print(R) R=a-b;
else: System.out.println(R);
print(“Error”) break;
case "*":
R=a*b;
System.out.println(R);
break;
case "/":
R=a/b;
System.out.println(R);
break;
default:
System.out.println("error");
}
}
}
9
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
Series – Sumatorias
Python Java
n=int(input()) import java.util.*;
ini=2 public class Main {
d1=4 public static void main(String[] args) {
d2=4 Scanner leer=new Scanner(System.in);
signo=-1 int n=leer.nextInt();
for _ in range(1, n+1): int ini=2, d1=4, d2=4, signo=-1;
print(ini," ",d1, end=” “) for (int i = 1; i <= n; i++) {
ini+=d1 System.out.print(ini+" ");
d1+=signo*2 ini+=d1;
if d1==2: d1+=signo*2;
signo*=(-1) if (d1==2) { signo*=(-1); d2+=2;
d2+=2 }
if d1==d2: if (d1==d2){ signo*=(-1);
signo*=(-1) }
} } }
10
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
Python Java
n=int(input()) import java.util.*;
prim=2; ini=-1; cont=0; sw=0; k=2 public class Main {
while sw==0: public static void main(String[] args) {
if prim%k==0: Scanner leer=new Scanner(System.in);
if cont%2==0: int n=leer.nextInt();
print(ini, end=" ") int prim=2, ini=-1, cont=0, sw=0, k=2;
ini=ini+4 while (sw==0) {
cont=cont+1 if (prim%k==0) {
else: if(cont%2==0) {
if k==prim: System.out.print(ini+" ");
print(prim, end=" ") ini=ini+4;
cont=cont+1 cont++;
prim=prim+1 }else {
k=2 if(k==prim) {
else: System.out.print(prim+" ");
prim=prim+1 cont++;
k=2 prim++;
if cont==n: k=2;
sw=1 } else { prim++; k=2;
else: }
k=k+1 }
if(cont==n) { sw=1;
11
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
}
}
else{ k++;
}
} } }
Python Java
n = int(input()) import java.util.*;
num=1; t=1; sw=1; sw1=1; rnum=0 class Main {
for i in range(1, n+1): public static void main(String args[]) {
if sw==2: Scanner leer=new Scanner(System.in);
print(t, end=" ") int n=leer.nextInt();
sw=0 int num=1, t=1, sw=1, sw1=1, rnum=0;
if sw1==0: for(int i=1; i<=n; i++)
t=t+1 { if(sw==2)
else: { System.out.print(t+" ");
sw1=0 sw=0;
t=1 if(sw1==0){ t++; }
else: else{ sw1=0; t=1; }
print(num, end=" ") }
num=num+1 else { System.out.print(num+" ");
sw=sw+1 num++; sw++; }
} } }
12
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
Python Java
n = int(input()) import java.util.*;
x = int(input()) public class Main {
num=5 public static void main(String[] args) {
den=3 Scanner leer=new Scanner(System.in);
c=3 int n=leer.nextInt();
sum=0 int x=leer.nextInt();
for i in range(1, n+1): int num=5, den=3, c=3;
signo=1 double sum=0;
if i%2==0: for (int i = 1; i <=n; i++) {
signo=signo*-1 int signo=1;
sum=sum+signo*((x**num)/den) if (i%2==0) { signo=signo*-1; }
aux=num sum=sum+signo*((Math.pow(x, num))/den);
num=den int aux=num;
den=aux num=den;
c=c-1 den=aux;
if c==0: c=c-1;
num=num*2 if (c==0) { num=num*2; den=den+2; c=3; }
den=den+2 } System.out.println(sum);
c=3 } }
print(sum)
13
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
Python Java
n = int(input()) import java.util.*;
x = int(input()) public class ej4 {
#variables serie exponentes public static void main(String[] args) {
t1 = 1; k1 = 1 Scanner leer=new Scanner(System.in);
#variables serie denominador int n=leer.nextInt();
sum = 0; t2 = 1; k2 = 3; p = 1; fl2 = 1 int x=leer.nextInt();
#variables de signos(serie fibonacci) //variables serie exponentes
a = 1; b = 0; t3 = a + b; c = 0 int t1 = 1, k1 = 1;
sig = 1 //variables serie denominador
#acumulador de la suma int sum = 0, t2 = 1, k2 = 3, p = 1, fl2 = 1;
s=0 //variables de signos(serie fibonacci)
for i in range(1,n+1): int a = 1, b = 0, t3 = a + b, c = 0;
14
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
DESCOMPOSICION – LOTES
Enunciado - ejemplo Diagrama de flujo
Ejercicio 1
Hallar la suma de todos los dígitos
de un numero hasta que quede
solo un digito.
Ejemplo: 123456789 -> 45 -> 9
15
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
Python Java
n = int(input()) import java.util.Scanner;
suma = 0 public class ejer1 {
x=n public static void main(String[] args) {
while(x > 9): Scanner leer = new Scanner(System.in);
while(x != 0): int n,x,suma = 0,d;
d = x % 10 n = leer.nextInt();
suma = suma + d x = n;
x = x // 10 while(x > 9) {
x = suma while(x != 0) {
suma = 0 d = x % 10;
print(x) suma = suma + d;
x = x / 10;
}
x = suma;
System.out.println(x);
suma = 0;
}
} }
Corrida Python Corrida Java
123456789
45
9
Enunciado - ejemplo Diagrama de flujo
Ejercicio 2
Dado un numero n, hallar todos
los números capicúas contenidos
en el.
Ejemplo:
48711744 ->
4,8,7,7117,1,11,7,4,44,4
Python Java
import math import java.util.Scanner;
n = int(input()) public class ejer2 {
cd = int(math.log10(n)) + 1 public static void main(String[] args) {
p=1 Scanner leer = new Scanner(System.in);
while n != 0: int n,cd,p,d,orig,inv,po,dig;
d = n //(10 ** (cd-p)) n = leer.nextInt();
orig = d cd = (int)Math.log10(n) + 1;
inv = 0 p = 1;
po = 0 while(n != 0) {
16
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
17
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
Python Java
import math import java.util.Scanner;
n = int(input()) public class ejer3 {
aux = n public static void main(String[] args) {
nuevo = 0 Scanner leer = new Scanner(System.in);
guar = 0 int n,aux,nuevo,guar,cd,cd2,sw,dig1;
cd = int(math.log10(n))+1 n = leer.nextInt();
cd2 = cd aux = n;nuevo = 0;guar = 0;
sw = 1 cd = (int)Math.log10(n) + 1;
for i in range(cd2): cd2 = cd;sw = 1;
dig1 = aux // 10 ** (cd -1) for(int i = 1; i <= cd2; i++) {
if sw == 1 and dig1 % 2 == 0: dig1 = aux / ((int)Math.pow(10, cd-1));
nuevo = (nuevo * 10) + dig1 if(sw == 1 && dig1 % 2 == 0) {
guar = dig1 nuevo = (nuevo * 10) + dig1;
sw = 0 guar = dig1;
else: sw = 0;
if dig1 % 2 == 0: }else {
nuevo = nuevo * 10 + guar if(dig1 % 2 == 0) {
guar = dig1 nuevo = (nuevo * 10) + guar;
else: guar = dig1;
nuevo = nuevo * 10 + dig1 }else {
aux = aux % 10 ** (cd -1) nuevo = (nuevo * 10) + dig1;
cd = cd - 1 }
cd = cd2 }
aux = nuevo aux = aux % ((int)Math.pow(10, cd-1));
nuevo = 0 cd = cd - 1;
sw = 1 }
for i in range(cd2): cd = cd2; aux = nuevo;
dig1 = aux//10**(cd-1) nuevo = 0; sw = 1;
if dig1 % 2 == 0 and sw == 1: for(int i = 1; i <= cd2; i++) {
nuevo = nuevo * 10 + guar dig1 = aux /((int)Math.pow(10, cd-1));
sw = 0 if(dig1 % 2 == 0 && sw == 1) {
else: nuevo = (nuevo * 10) + guar;
nuevo = nuevo * 10 + dig1 }else {
aux = aux % 10**(cd-1) nuevo = (nuevo * 10) + dig1;
cd = cd - 1 }
print(nuevo) aux = aux % ((int)Math.pow(10, cd-1));
cd = cd -1;
}
System.out.println(nuevo);
} }
Corrida Python Corrida Java
7348293482994
7344893248992
13
18
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
Python Java
suma = 0 import java.util.Scanner;
x = int(input()) public class ejer4 {
while x != -1: public static void main(String[] args) {
a = -1 Scanner leer = new Scanner(System.in);
b=1 int x,suma,a,b,ter;
ter = a + b suma = 0;
while ter < x: x = leer.nextInt();
a=b while(x != -1) {
b = ter a = -1;b = 1; ter = a + b;
ter = a + b while(ter < x) {
if ter == x: a = b; b = ter;
print(x) ter = a + b;
suma = suma + x }
x = int(input()) if(ter == x) {
print(suma) System.out.println(x);
suma = suma + x;
}
x = leer.nextInt();
}
System.out.println(suma);
}
}
Corrida Python Corrida Java
21
21
7
8
8
-1
29
19
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
Python Java
n = int(input()) import java.util.Scanner;
for i in range(n): public class ejer5 {
x = int(input()) public static void main(String[] args) {
c=0 Scanner leer = new Scanner(System.in);
k=2 int n,x,c,k,p,lim;
p=1 n = leer.nextInt();
while p != x : for(int i = 1; i <= n; i++) {
p=p+1 x = leer.nextInt();
k=2 c = 0; k = 2; p = 1;
while p % k != 0 and k <= p // 2: while(p != x) { p = p + 1; k = 2;
k=k+1 while(p % k != 0 && k <= p/2) {
if k > p//2: k = k + 1;
c=c+1 }
lim = c+1 if(k > p/2) c = c + 1;
while (c < lim): }
p=p+1 lim = c + 1;
k=2 while(c < lim) { p = p + 1; k = 2;
while p % k != 0 and k <= p // 2: while (p % k != 0 && k <= p / 2) {
k=k+1 k = k + 1; }
if k > p // 2: if(k > p /2) c = c + 1;
c=c+1 }
print(p) System.out.println(p);
} } }
Corrida Python Corrida Java
3
1112
1117
24
29
55
59
Funciones
20
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
Python Java
def buscar_el_7(nu): import java.util.*;
sw = 0 public class Main {
d=0 public static void main(String[] args) {
while nu > 0: Scanner lee=new Scanner(System.in);
d = nu % 10 int nu=lee.nextInt();
if d==7: if (buscar_el_7(nu)==1) {
sw = 1 System.out.println(1);
nu = nu//10 }else {
return sw System.out.println(0);
}
nu = int(input()) }
if(buscar_el_7(nu)==1): private static int buscar_el_7(int nu) {
print("1") int sw=0, d=0;
else: while (nu>0) {
print("0") d=nu%10;
if (d==7) {
sw=1;
}
nu=nu/10;
}
return sw;
}
}
Corrida Python Corrida Java
321996127 1321651
1 0
Python Java
def esFibo(x): import java.util.*;
a =-1 public class Main {
b=1 public static void main(String[] args) {
c=0 Scanner lee=new Scanner(System.in);
21
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
sw = 0 int n=lee.nextInt();
while c < x: for (int i = 1; i <= n; i++) {
c=a+b int x=lee.nextInt();
a=b int r=esFibo(x);
b=c if(r==1) {
if c == x: System.out.println("--> "+x);
sw = 1 }
return sw }
}
n = int(input()) private static int esFibo(int x) {
for i in range(n): int a=-1,b=1,c=0,sw=0;
x = int(input()) while(c<x){
r = esFibo(x) c=a+b;
if r == 1: a=b;
print("-->",x) b=c;
}
if(c==x) {
sw=1;
}
return sw;
}
}
Corrida Python Corrida Java
5
22
10
1
1
2
5
5
22
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
Python Java
def f1(a): import java.util.*;
nueN = 0 public class Main {
p=1 public static void main(String[] args) {
while a > 0: Scanner lee=new Scanner(System.in);
dig = a % 10 int n=lee.nextInt();
a = a // 10 for (int i = 1; i <= n; i++) {
if dig % 2 == 0: int x=lee.nextInt();
nueN = nueN + (dig*p) int n1=f1(x); int n2=f2(x);
p = p * 10 System.out.println(n2+"\t"+n1);
return nueN } }
private static int f1(int a) {
def f2(a): int nueN=0, p=1;
nueN = 0 while (a>0) { int dig=a%10; a=a/10;
p=1 if(dig%2==0) {
while a > 0: nueN=nueN+(dig*p);
dig = a % 10 p=p*10; }
a = a // 10 }
if dig % 2 == 1: return nueN;
nueN = nueN + (dig*p) }
p = p * 10 private static int f2(int a) {
return nueN int nueN=0,p=1;
while (a>0) { int dig=a%10; a=a/10;
n = int(input()) if(dig%2==1) {
for i in range(n): nueN=nueN+(dig*p);
x = int(input()) p=p*10;
n1 = f1(x) } }
n2 = f2(x) return nueN;
print(n2,"\t",n1) } }
Corrida Python Corrida Java
23
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
3
24054
5 2404
354789
3579 48
6273891
7391 628
Python Java
import math import java.util.*;
def fact(w): public class Main {
f=1 public static void main(String[] args) {
for i in range(1,w+1): Scanner lee=new Scanner(System.in);
f = f*i int n=lee.nextInt();
return f int x=lee.nextInt();
double s=0;
def serie(w): for(int i=1; i<=n; i++){
ini = 2 int fac= fact(x);
d1 = 4 int d=serie(i);
d2 = 4 s=s+(double)Math.pow((fac),
signo = -1 i)/(double)d;
for i in range(1,w): x=x+1;
ini += d1 }
d1 += signo*2 System.out.println(s);
24
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
if d1 == 2: }
signo *= (-1) static int fact(int w){
d2 += 2 int f=1;
if d1 == d2: for(int i=1; i<=w; i++){
signo *= (-1) f=f*i;
return ini }
return(f);
n = int(input()) }
x = int(input()) static int serie(int w){
s=0 int ini=2, d1=4, d2=4, signo=-1;
for i in range(1,n+1): for (int i = 1; i < w; i++) {
fac = fact(x) ini+=d1;
d = serie(i) d1+=signo*2;
s = s + (math.pow(fac,i)/d) if (d1==2) { signo*=(-1); d2+=2; }
x=x+1 if (d1==d2) { signo*=(-1); }
print(s) }
return ini;
} }
Corrida Python Corrida Java
3
1
28.166666666666668
25
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
Python Java
def signo(w): import java.util.*;
p=2; c=1; k=2; a=0; b=2; x=1 ; j=2 public class Main {
for i in range(1,w+1): public static void main(String[] args) {
if a==b: Scanner lee=new Scanner(System.in);
while c<=j: Int n=lee.nextInt();
if p%k!=0 and k<=p/2: Int x=lee.nextInt();
k+=1 int p,num,fac,d,sig;
else: double s=0;
if k>p/2: for(int i=1; i<=n; i++){
c+=1; b=p p= primo(i);
p+=1; k=2 d=serie(i);
x=-x; a=1; j+=1 fac= fact(d);
else: num= fibo(i);
a+=1 sig=signo(i);
return x s=s+sig*(double)Math.pow(num,
def fibo(w): p)/(double)Math. pow (fac, x);
a=0; b=1; c=1 }
for i in range(1,w+1): System.out.println(s);
a=b; b=c; c=a+b }
return a static int primo(int w){
def serie(w): int p=2, cont=1, k=2, p1=0;
a=1; b=3; c=1; a1=0 while(cont<=w){
for i in range(1,w+1): if(p%k!=0 && k<=p/2){ k++;
a1=a }else{ if(k>p/2){ p1=p; cont++;
if a==b: }
if b==3: p++; k=2;
b=0; c=-c; a-=1 } }
else: return p1; }
b=3; c=-c static int fact(int w){
else: int f=1;
a=a+c for(int i=1; i<=w; i++){ f=f*i; }
if a==0: return(f);
a=1; b=3; c=-c }
return a1 static int serie(int w){
def fact(w): int a=1, b=3, c=1,a1=0;
f=1 for(int i=1; i<=w; i++){ a1=a;
for i in range(1,w+1): if(a==b){
f=f*i if(b==3){ b=0; c=-c; a--; }
else{ b=3; c=-c; }
return f }
else{ a=a+c;
def primo(w): }
p=2 if(a==0){ a=1; b=3; c=-c;
cont=1 }
k=2 }
p1=0 return a1;
while cont<=w: }
if p%k!=0 and k<=p/2: static int fibo(int w){
k+=1 int a=0, b=1, c=1;
else: for(int i=1; i<=w; i++){ a=b; b=c; c=a+b; }
if k>p/2: return a;
26
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
p1=p }
cont+=1 static int signo(int w){
p+=1 int p=2, c=1,k=2,a=0,b=2,x=1, j=2;
k=2 for(int i=1; i<=w; i++){
return p1 if(a==b){
while(c<=j){
n = int(input()) if(p%k!=0&& k<=p/2){ k++; }
x = int(input()) else{ if(k>p/2){ c++; b=p; }
s=0 p++; k=2; }
for i in range(1,n+1): } x=-x; a=1; j++; }
p=primo(i) else{ a++;}
d=serie(i) }
fac=fact(d) return x;
num=fibo(i) } }
sig=signo(i)
s=s+sig*(((num**p)/(fac**x)))
print(s)
RECURSIVIDAD
Enunciado - ejemplo Diagrama de flujo
Ejercicio 1
Dado un lote de n números
mostrar la cantidad de dígitos
de cada uno usando funciones
recursivas.
Ejemplo:
N=4
2345 -> 4
345423 -> 6
23 -> 2
3245434 -> 7
Python Java
def cantDig(n): import java.util.Scanner;
if n == 0: public class Ejer1 {
27
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
28
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
Python Java
def fibN(n): import java.util.Scanner;
if n == 1: public class Ejer2 {
return 0 public static int fibN(int n) {
elif n == 2: if(n == 1) {
return 1 return 0;
else: }else {
return fibN(n-1) + fibN(n-2) if(n == 2)
n = int(input()) return 1;
nf = fibN(n) else
print(nf) return fibN(n-1) + fibN(n-2);
}
}
public static void main(String[] args) {
Scanner leer = new Scanner(System.in);
int n,nf;
n = leer.nextInt();
nf = fibN(n);
System.out.println(nf);
}
}
Corrida Python Corrida Java
6
5
Python Java
def harmonica(n): import java.util.Scanner;
if n == 1: public class Ejer3 {
return 1 public static double harmonica(double n) {
else: if(n == 1) {
return (1 / n) + harmonica(n-1) return 1;
n = int(input()) }else {
s = harmonica(n) return (1/n)+harmonica(n-1);
print(s) }
}
public static void main(String[] args) {
29
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
Python Java
def bina(n): import java.util.Scanner;
if n == 0: public class Ejer4 {
return 0 public static long bina(int n) {
else: if(n == 0) {
return (n%2) + 10 * bina(n//2) return 0;
n = int(input()) }else {
for i in range(n): return (n%2) + 10 * (bina(n/2));
x = int(input()) }
b = bina(x) }
print(b) public static void main(String[] args) {
Scanner leer = new Scanner(System.in);
int n,x;
long b;
n = leer.nextInt();
for(int i = 0; i < n; i++) {
x = leer.nextInt();
b = bina(x);
System.out.println(b);
}
}
30
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
}
Corrida Python Corrida Java
3
4
100
7
111
16
10000
Enunciado - ejemplo Diagrama de flujo
Ejercicio 5
Dado dos números mostrar el
máximo común divisor usando
funciones recursivas.
Ejemplo:
12,30 -> 6
Python Java
def maxComDiv(a,b): import java.util.Scanner;
if a > b: public class Ejer5 {
mayor = a public static int maxComDiv(int a, int b) {
menor = b int mayor,menor;
else: if (a > b) {
mayor = b mayor = a;
menor = a menor = b;
if menor == 0: }else {
return mayor mayor = b;
elif menor == 1: menor = a;
return 1 }
else: if(menor == 0) {
return return mayor;
maxComDiv(menor,mayor%menor) }else {
a = int(input()) if(menor == 1) {
b = int(input()) return 1;
m = maxComDiv(a,b) }else {
print(m) return maxComDiv(menor, mayor %
menor);
}
}
31
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
}
public static void main(String[] args) {
Scanner leer = new Scanner(System.in);
int a,b,m;
a = leer.nextInt();
b = leer.nextInt();
m = maxComDiv(a, b);
System.out.println(m);
}
}
Corrida Python Corrida Java
12 33
30 11
6 11
Procedimientos – Cadenas
Python Java
import math import java.util.*;
def rotarRveces(x,r): public class Main {
for i in range (1, r+1): public static void main(String[] args) {
lon = int(math.log10(x)) Scanner lee=new Scanner(System.in);
d1 = x//int(math.pow(10, lon)) int n=lee.nextInt();
x = x%int(math.pow(10, lon)) while (n>0) {
x = (x*10)+d1 int x=lee.nextInt();
print(x) int r=lee.nextInt();
rotarRveces(x,r);
n=int(input()) n-=1;
while n>0: }
x=int(input()) }
r=int(input()) private static void rotarRveces(int x, int r) {
rotarRveces(x,r) int lon, d1;
32
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
Python Java
def MayMenSum(x,ma,me,su): import java.util.Scanner;
ma=x%10 public class Main {
me=ma public static void main(String[] args) {
while x!=0: Scanner lee=new Scanner(System.in);
d=x%10 int n=lee.nextInt();
su=su+d for (int i = 1; i <=n; i++) {
if d>ma: int x=lee.nextInt();
ma=d int ma=0,me=0,su=0;
if d<ma: MayMenSum(x,ma,me,su);
me=d }
x=x//10 }
33
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
print(ma,me,su) static void MayMenSum(int x, int ma, int me, int su) {
n=int(input()) ma=x%10;
for i in range(1,n+1): me=ma;
x=int(input()) while (x!=0) {
ma=0 int d=x%10;
me=0 su=su+d;
su=0 if (d>ma)
MayMenSum(x,ma,me,su) ma=d;
if (d<ma)
me=d;
x=x/10;
}
System.out.println(ma+" "+me+" "+su);
}
}
Corrida Python Corrida Java
1
123459121
9 1 28
34
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
Python Java
def invPal(pal): import java.util.Scanner;
ele=len(pal) public class Main {
w="" public static void main(String[] args) {
for i in range(ele,0,-1): Scanner lee=new Scanner(System.in);
y=pal[i-1] String cad=lee.nextLine();
w=w+y int cantPal= contPal(cad);
return w String w="";
def obtPal(cad, i): for (int i = 1; i <= cantPal; i++) {
cad=cad+" " String pal=obtPal(cad,i);
cont=0 w=w+invPal(pal)+" ";
ele=len(cad) }
v="" System.out.println(w);
w=""
for j in range(1,ele+1): }
y=cad[j-1] private static String invPal(String pal) {
if y==" ": int ele=pal.length();
cont=cont+1 String w="";
if cont==i: for (int i = ele; i >= 1; i--) {
w=v String y = pal.substring(i-1,i);
v="" w=w+y;
else: }
v=v+y return w;
return w }
def contPal(cad): private static String obtPal(String cad, int i) {
cad=cad+" " cad=cad+" ";
cont=0 int cont=0;
ele=len(cad) int ele=cad.length();
for i in range(1,ele+1): String v="", w="";
y=cad[i-1] for (int j = 1; j <= ele; j++) {
if y==" ": String y = cad.substring(j-1,j);
35
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
Python Java
36
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
37
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
Python Java
def cambVocal(pal, voc): import java.util.Scanner;
ele=len(pal) public class Main {
w="" public static void main(String[] args) {
for i in range(1,ele+1): Scanner lee=new Scanner(System.in);
y=pal[i-1] String cad=lee.nextLine();
if esVocal(y)==1: int ca=contar(cad,"a");
w=w+voc int ce=contar(cad,"e");
else: int co=contar(cad,"o");
w=w+y String vocEle=vocEle(ca,ce,co);
return w String newcad=cambVocal(cad, vocEle);
System.out.println("a: "+ca+" e: "+ce+" o: "+co);
def esVocal(cad): System.out.println(newcad);
k=0 }
if cad=="a" or cad=="e" or cad=="i" or private static String cambVocal(String pal, String voc) {
cad=="o" or cad=="u": int ele= pal.length();
k=1 String w="";
return k for (int i = 1; i <= ele; i++) {
String y = pal.substring(i-1,i);
def vocEle(ca, ce, co): if(esVocal(y)==1) {
may=ca w=w+voc;
ele="a" }else { w=w+y; }
if ce>may: }
ele="e" return w;
may=ce }
if co>may: private static int esVocal(String cad) {
ele="o" int k=0;
return ele if (cad.equals("a") || cad.equals("e")
|| cad.equals("i") || cad.equals("o")
38
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
39
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
Python Java
def llenar(): import java.util.Scanner;
for i in range(n): public class ejer1 {
x = int(input()) public static int v[];
v[i] = x public static int n;
def mostrar(): public static void llenar() {
for i in range(n): int i,x;
print(v[i],end=" ") Scanner leer = new Scanner(System.in);
print() for(i = 0; i < n; i++) { x = leer.nextInt(); v[i] = x; }
def llevaFinal(k): }
pos = 0 public static void mostrar() {
for i in range(n): int i;
if v[i] != k: for(i = 0; i < n; i++) { System.out.print(v[i]+" "); }
v[pos] = v[i] System.out.println();
pos += 1 }
while pos < n: public static void llevarFinal(int k) {
v[pos] = k int pos = 0, i;
pos += 1 for(i = 0; i < n; i++) {
n = int(input()) if(v[i] != k) {v[pos] = v[i]; pos = pos + 1; }
v = [0]*n }
llenar() while (pos < n) { v[pos] = k; pos = pos + 1; }
k = int(input()) }
mostrar() public static void main(String[] args) {
llevaFinal(k) Scanner leer = new Scanner(System.in);
mostrar() n = leer.nextInt();
v = new int[n];
llenar();
int k = leer.nextInt();
mostrar();
llevarFinal(k);
mostrar();
} }
Corrida Python Corrida Java
6
3
3
6
4
3
3
2
364332
642333
Enunciado - ejemplo Diagrama de flujo
40
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
Ejercicio 2
Dado un vector con N
elementos, encontrar los
elementos de la serie Fibonacci
de un vector y reemplazarlos
por el siguiente termino
superior de la serie Fibonacci.
Ejemplo:
6
3 4 8 21 4 6
5 4 13 34 4 6
Python Java
def llenar(): import java.util.Scanner;
for i in range(n): public class ejer2 {
x = int(input()) public static int v[];
l[i] = x public static int n;
def mostrar(): public static void llenar() {
for i in range(n): int i,x;
print(l[i],end=" ") Scanner leer = new Scanner(System.in);
print() for(i = 0; i < n; i++) {
def fiboSup(x): x = leer.nextInt();
a = -1 v[i] = x;
b=1 }
ter = a + b }
while ter < x+1: public static void mostrar() {
a=b int i;
b = ter for(i = 0; i < n; i++) {
ter = a + b System.out.print(v[i]+" ");
return ter }
def esFibo(x): System.out.println();
a = -1 }
b=1 public static int fiboSup(int x) {
ter = a + b int a,b,ter, i;
k=0 a = -1; b = 1; ter = a + b;
while ter < x: ter = 0;
a=b while (ter < x + 1) {
b = ter a = b;
ter = a + b b = ter;
if ter == x: ter = a + b;
k=1 }
return k return ter;
n = int(input()) }
l = [0]*n public static int esFibo(int x) {
llenar() int a,b,ter,i,k;
mostrar() a = -1; b = 1; ter = a + b;
41
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
42
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
Python Java
def esPrimo(x): import java.util.Scanner;
k=0 public class ejer3 {
if x == 1: public static int v[];
k=0 public static int n;
else: public static int esPrimo(int x) {
d=2 int k = 0, d;
while x % d != 0 and d <= x // 2: if(x == 1)
d=d+1 k = 0;
if d > x // 2: else {
k=1 d = 2;
return k while(x % d != 0 && d <= x / 2)
def llenarPrimos(): d = d + 1;
for i in range(1,n+1): if (d > x / 2)
if esPrimo(i) == 1: k = 1;
v[i] = 1 }
else: return k;
v[i] = 0 }
def mostrar(): public static void llenarPrimos() {
for i in range(1,n+1): int i;
print(v[i],end=" ") for(i = 1; i < n+1; i++) {
print() if(esPrimo(i) == 1)
n = int(input()) v[i] = 1;
v = [0]*(n+1) else {
llenarPrimos() v[i] = 0;
mostrar() }
}
}
public static void mostrar() {
int i;
for(i = 1; i < n+1; i++) {
System.out.print(v[i]+" ");
}
System.out.println();
}
public static void main(String[] args) {
Scanner leer = new Scanner(System.in);
n = leer.nextInt();
v = new int[n+1];
llenarPrimos();
mostrar();
}
}
Corrida Python Corrida Java
12
011010100010
Enunciado - ejemplo Diagrama de flujo
Ejercicio 4
Dado un vector con N
elementos eliminar los números
que sean capicúas.
Ejemplo
7
44, 7, 56, 121, 3333, 67, 34
56, 67, 34
43
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
Python Java
def llenar(): import java.util.Scanner;
for i in range(n): public class ejer4 {
x = int(input()) public static int v[];
v[i] = x public static int n;
def mostrar(): public static void llenar() {
for i in range(n): int i,x;
print(v[i],end=" ") Scanner leer = new Scanner(System.in);
print() for(i = 0; i < n; i++) {
def esCap(x): x = leer.nextInt();
r=x v[i] = x;
nue = 0 }
k=0 }
while r != 0: public static void mostrar() {
d = r % 10 int i;
nue = nue*10 + d for(i = 0; i < n; i++) {
r //= 10 System.out.print(v[i]+" ");
if nue == x: }
k=1 System.out.println();
return k }
def eliminarCapicuas(): public static int esCap(int x) {
global n int r,nue,k,d;
i=0 r = x; nue = 0; k = 0;
while i < n: while (r != 0) {
if esCap(v[i]) == 1: d = r % 10;
for j in range(i,n-1): nue = nue * 10 + d;
v[j] = v[j+1] r = r / 10;
n -= 1 }
else: if(nue == x)
i += 1 k = 1;
return k;
n = int(input()) }
v = [0]*n public static void eliminarCapicuas() {
llenar() int i = 0,j;
mostrar() while(i < n) {
eliminarCapicuas() if(esCap(v[i]) == 1) {
mostrar() for(j = i; j < n-1; j++)
v[j] = v[j+1];
n = n - 1;
}else
i = i + 1;
}
44
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
}
public static void main(String[] args) {
Scanner leer = new Scanner(System.in);
n = leer.nextInt();
v = new int[n];
llenar();
mostrar();
eliminarCapicuas();
mostrar();
}
}
Corrida Python Corrida Java
6
11
121
33
45
65
44
11 121 33 45 65 44 45 65
45
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
Python Java
def llenar(): import java.util.Scanner;
for i in range(n): public class ejer5 {
x = int(input()) public static int v[];
v[i] = x public static int n;
def mostrar(): public static void llenar() {
for i in range(n): int i,x;
print(v[i],end=" ") Scanner leer = new Scanner(System.in);
print() for(i = 0; i < n; i++) {
def esPrimo(x): x = leer.nextInt();
k=0 v[i] = x;
if x == 1: }
k=0 }
else: public static void mostrar() {
d=2 int i;
while x % d != 0 and d <= x // 2: for(i = 0; i < n; i++) {
d=d+1 System.out.print(v[i]+" ");
if d > x // 2: }
k=1 System.out.println();
return k }
def rotar(): public static int esPrimo(int x) {
fl1 = 0 int k = 0, d;
fl2 = 0 if(x == 1)
pos1 = 0 k = 0;
pos2 = 0 else {
x1 = 0 d = 2;
for i in range(n): while(x % d != 0 && d <= x / 2)
if esPrimo(v[i]) == 1: d = d + 1;
if fl1 == 0: if (d > x / 2)
pos1 = i k = 1;
x1 = v[i] }
fl1 = 1 return k;
else: }
y1 = v[i] public static void rotar() {
v[i] = x1 int fl1 = 0,fl2 = 0,pos1 = 0,pos2 = 0,x1 = 0,x2 = 0,y1,i;
x1 = y1 for(i = 0; i < n; i++) {
else: if(esPrimo(v[i]) == 1) {
46
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
if fl2 == 0: if(fl1 == 0) {
x2 = v[i] pos1 = i;
pos2 = i x1 = v[i];
fl2 = 1 fl1 = 1;
else: }else {
v[pos2] = v[i] y1 = v[i];
pos2 = i v[i] = x1;
if fl1 == 1: x1 = y1;
v[pos1] = x1 }
if fl2 == 1: }else {
v[pos2] = x2 if(fl2 == 0) {
n = int(input()) x2 = v[i];
v = [0]*n pos2 = i;
llenar() fl2 = 1;
mostrar() }else {
rotar() v[pos2] = v[i];
mostrar() pos2 = i;
}
}
if(fl1 == 1)
v[pos1] = x1;
if(fl2 == 1)
v[pos2] = x2;
}
}
public static void main(String[] args) {
Scanner leer = new Scanner(System.in);
n = leer.nextInt();
v = new int[n];
llenar();
mostrar();
rotar();
mostrar();
}
}
Corrida Python Corrida Java
8
3
4
6
7
8
11
31
23
3 4 6 7 8 11 31 23
23 6 8 3 4 7 11 31
47
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
Sort - search
Python Java
def llenar(v,n): import java.util.Scanner;
for i in range(1,n+1): public class Main {
v[i]=int(input()) public static Scanner lee=new Scanner(System.in);
def mostrar(v,n): public static void main(String[] args) {
for i in range(1,n+1): int n=lee.nextInt();
print(v[i],end=" ") int k=lee.nextInt();
def ordenar(posI,lim,v): int v[]=new int[n+1];
for i in range(posI+1, lim+1): int aux=k;
ind=v[i] llenar(v,n);
a=i-1 while (aux<=n) {
while a>=posI and v[a]>ind: ordenar(aux-k+1,aux,v);
v[a+1]=v[a] aux=aux+k;
a=a-1 }
v[a+1]=ind mostrar(v,n);
n=int(input()) }
k=int(input()) private static void ordenar(int posI, int lim, int[] v) {
v =[0]*(n+1) for (int i = posI+1; i <=lim; i++) {
aux=k int ind=v[i];
llenar(v,n) int a=i-1;
while aux<=n: while(a>=posI && v[a]>ind) {
ordenar(aux-k+1,aux,v) v[a+1]=v[a];
aux=aux+k a=a-1;
mostrar(v,n) }
v[a+1]=ind;
}
}
private static void llenar(int[] v, int n) {
for (int i =1; i <=n; i++) {
v[i]=lee.nextInt();
}
48
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
}
private static void mostrar(int[] v, int n) {
for (int i =1; i <=n; i++) {
System.out.print(v[i]+" ");
}
}
}
Corrida Python Corrida Java
10
5
1
98
4
5
3
4
7
9
1
0
1 3 4 5 98 0 1 4 7 9
Enunciado - ejemplo Diagrama de flujo
EJERCICIO 2
Dado un vector v que contiene n
números, dividido en sectores de k
elementos. Se pide ordenar los sectores
pares de forma ascendente y los
sectores impares en forma descendente.
Entrada
10
3
1,2,3,4,5,6,7,8,9,10
Salida
3 2 1 4 5 6 9 8 7 10
49
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
Python Java
def llenar(v,n): import java.util.Scanner;
for i in range(1,n+1): public class Main {
v[i]=int(input()) public static Scanner lee=new Scanner(System.in);
def mostrar(v,n): public static void main(String[] args) {
for i in range(1,n+1): int n=lee.nextInt();
print(v[i],end=" ") int k=lee.nextInt();
def ordMenMay(posI,lim,v): int v[]=new int[n+1];
for i in range(posI, lim): int p=k;
for j in range(i+1, lim+1): int cont=0;
if v[i]>v[j]: llenar(v,n);
aux=v[i] while (p<=n) {
v[i]=v[j] cont=cont+1;
v[j]=aux if ((cont % 2)==0) {
def ordMayMen(posI,lim,v): ordMenMay(p-k+1,p,v);
for i in range(posI, lim): }else {
for j in range(i+1, lim+1): ordMayMen(p-k+1,p,v);
if v[i]<v[j]: }
aux=v[i] p=p+k;
v[i]=v[j] }
v[j]=aux mostrar(v,n);
n=int(input()) }
k=int(input()) private static void ordMenMay(int posI, int lim, int[] v) {
v =[0]*(n+1) for (int i = posI; i <lim; i++) {
p=k for (int j = i+1; j <lim+1; j++) {
cont=0 if (v[i]>v[j]) {
llenar(v,n) int aux=v[i];
while p<=n: v[i]=v[j];
cont=cont+1 v[j]=aux;
if (cont % 2)==0: }
ordMenMay(p-k+1,p,v) }
else: }
ordMayMen(p-k+1,p,v) }
p=p+k private static void ordMayMen(int posI, int lim, int[] v) {
mostrar(v,n) for (int i = posI; i <lim; i++) {
for (int j = i+1; j <lim+1; j++) {
if (v[i]<v[j]) {
int aux=v[i];
v[i]=v[j];
v[j]=aux;
}
}
} }
private static void llenar(int[] v, int n) {
for (int i =1; i <=n; i++) {
v[i]=lee.nextInt();
} }
private static void mostrar(int[] v, int n) {
for (int i =1; i <=n; i++) {
System.out.print(v[i]+" ");
} } }
50
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
Python Java
def ordenadoF(nv,v,n): import java.util.*;
for i in range(1, n): public class Main {
for j in range(i+1, n+1): public static Scanner lee = new Scanner(System.in);
if nv[i]==nv[j] and v[i]>v[j]: public static void main(String[] args) {
temp = v[i] int n=lee.nextInt();
51
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
52
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
6
161
1111
26
2
2
4
2 2 4 1111 26 161
Enunciado - ejemplo Diagrama de flujo
EJERCICIO 4 (búsqueda binaria)
Dado un vector de tamaño n con
elementos ordenados, se desea verificar
si el numero x se encuentra en el vector.
Entrada
5
3
1, 3, 6, 7, 9
Salida
“Si se encuentra”
Python Java
def llenar(v,n): package sort_search;
for i in range(1,n+1): import java.util.Scanner;
v[i]=int(input()) public class Ej44 {
def busquedaBinaria(v,n,x): public static Scanner lee=new Scanner(System.in);
inf=1 public static void main(String[] args) {
sup=n int n=lee.nextInt();
medio=(inf+sup)//2 int x=lee.nextInt();
sw=0 int v[]=new int[n+1];
while inf<=sup and v[medio]!=x: llenar(v,n);
if x<v[medio]: if(busquedaBinaria(v,n,x)==1) {
sup=medio-1 System.out.println("Si se encuentra");
else: }else {
inf=medio+1 System.out.println("No se encuentra");
medio=(inf+sup)//2 }
if x==v[medio]: }
sw=1 private static int busquedaBinaria(int[] v, int n, int x) {
return sw int inf=1;
n=int(input()) int sup=n;
x=int(input()) int medio=(inf+sup)/2;
v =[0]*(n+1) int sw=0;
llenar(v,n) while (inf<=sup && v[medio]!=x) {
if(busquedaBinaria(v,n,x)==1): if(x<v[medio]) {
print("Si se encuentra") sup=medio-1;
else: }else {
print("No se encuentra") inf=medio+1;
}
medio=(inf+sup)/2;
}
53
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
if(x==v[medio]) {
sw=1;
}
return sw;
}
private static void llenar(int[] v, int n) {
for (int i =1; i <=n; i++) {
v[i]=lee.nextInt();
}
}
}
Corrida Python Corrida Java
5
3
4
5
6
7
8
No se encuentra
Enunciado - ejemplo Diagrama de flujo
EJERCICIO 5
Dado un vector de n elementos
ordenados, y k números, se pide mostrar
la sumatoria de los elementos del vector,
que sean menores o iguales de cada k-
esimo número. Se pide implementar
búsqueda binaria.
Entrada
7 (n) tamaño del vector
1,2,3,4,5,6,7
3 (k) cantidad de numeros
3,7,2
Salida
36
7 28
23
Python Java
def llenar(v,n): package sort_search;
for i in range(1,n+1): import java.util.Scanner;
v[i]=int(input()) public class Main {
def sumarElem(v,n): public static Scanner lee=new Scanner(System.in);
aux=0 public static void main(String[] args) {
for i in range(1,n+1): int n=lee.nextInt();
aux+=v[i] int v[]=new int[n+2];
w[i]=aux int w[]=new int[n+2];
def busquedaBinaria(v,n,ki): llenar(v,n);
inf=1 sumarElem(v,w,n);
sup=n int k = lee.nextInt();
medio=(inf+sup)//2 for (int i = 1; i <=k; i++) {
while inf<=sup and v[medio]!=ki: int ki=lee.nextInt();
if ki<v[medio]: busquedaBinaria(v,w,n,ki);
sup=medio-1 }
54
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
else:
inf=medio+1 }
medio=(inf+sup)//2 private static void llenar(int[] v, int n) {
print(medio,w[medio]) for (int i =1; i <=n; i++) {
v[i]=lee.nextInt();
n = int(input()) }
v = [0]*(n+1) }
w = [0]*(n+1) private static void sumarElem(int[] v, int[] w, int n) {
llenar(v,n) int aux=0;
sumarElem(v,w,n) for (int i =1; i <=n; i++) {
k=int(input()) aux+=v[i];
for i in range(1,k+1): w[i]=aux;
ki=int(input()) }
busquedaBinaria(v,w,n,ki) }
private static void busquedaBinaria(int[] v, int[] w, int n, int ki) {
int inf=1;
int sup=n;
int medio=(inf+sup)/2;
while (inf<=sup && v[medio]!=ki) {
if(ki<v[medio]) {
sup=medio-1;
}else {
inf=medio+1;
}
medio=(inf+sup)/2;
}
System.out.println(medio+" "+w[medio]);
}
}
Corrida Python Corrida Java
5
1
2
3
4
5
3
1
11
6
5 15
3
36
55
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
MATRICES
Enunciado - ejemplo Diagrama de flujo
Ejercicio 1
Dado una matriz de N por M, mostrar el mayor de cada columna.Ejemplo: N = 3,M = 4
4 4 4 43
3 23 2 3
6 6 4 6
Salida:
6
23
4
43
Python Java
def llenar(): import java.util.Scanner;
for i in range(n): public class ejer1 {
for j in range(m): public static int v[][];
v[i][j] = int(input()) public static int n,m;
def mostrar(): public static void llenar() {
for i in range(n): int i,j;
for j in range(m): Scanner leer = new Scanner(System.in);
print(v[i][j],end="\t") for(i = 0; i < n; i++) {
print() for(j = 0; j < m; j++) {
def mostrarMayorCol(): v[i][j] = leer.nextInt();
for i in range(m): } } }
may = 0 public static void mostrar() {
for j in range(n): int i,j;
if v[j][i] > may: for(i = 0; i < n; i++) {
may = v[j][i] for(j = 0; j < m; j++) {
print(may) System.out.print(v[i][j]+"\t");
n = int(input()) }
m = int(input()) System.out.println();
v = [[0 for i in range(m)] for j in } }
range(n)] public static void mostrarMayorCol() {
llenar() int i,j,may;
mostrar() for(i = 0; i < m; i++) {
mostrarMayorCol() may = 0;
56
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
23
54
34
57
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
Python Java
def llenarBanda(): import java.util.Scanner;
c=1 public class Ejer2 {
for i in range(n): public static int v[][];
v[i][n+1-i-2] = c public static int n,m;
c=c+1 public static void llenarBanda() {
v[i][n+1-i-1] = c int i,j,c=1;
c=c+1 for(i = 0; i < n; i++) {
v[i][n+1-i] = c v[i][n+1-i-2] = c;
c=c+1 c = c + 1;
def mostrar(): v[i][n+1-i-1] = c;
for i in range(n): c = c + 1;
for j in range(n+2): v[i][n+1-i] = c;
print(v[i][j],end="\t") c = c + 1;
print() } }
n = int(input()) public static void mostrar() {
v = [[0 for i in range(n+2)] for j in range(n)] int i,j;
llenarBanda() for(i = 0; i < n; i++) {
mostrar() for(j = 0; j < n+2; j++) {
System.out.print(v[i][j]+"\t");
}
System.out.println();
} }
public static void main(String[] args) {
Scanner leer = new Scanner(System.in);
n = leer.nextInt();
v = new int[n][n+2];
llenarBanda();
mostrar();
} }
58
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
Python Java
def llenarForma(): import java.util.Scanner;
c=1 public class ejer3 {
for i in range(n): public static int v[][];
v[i][i] = c public static int n;
c=c+1 public static void llenarForma() {
for i in range(n): int i,j,c=1;
for j in range(n): for(i = 0; i < n; i++) {
if i != j: v[i][i] = c;
v[i][j] = c c = c + 1;
c=c+1 }
def mostrar(): for(i = 0; i < n; i++) {
for i in range(n): for(j = 0; j < n; j++) {
for j in range(n): if(i != j) {
print(v[i][j],end="\t") v[i][j] = c;
print() c = c + 1;
n = int(input()) }
v = [[0 for i in range(n)] for j in range(n)] }
llenarForma() } }
mostrar() public static void mostrar() {
int i,j;
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
System.out.print(v[i][j]+"\t");
}
System.out.println();
} }
public static void main(String[] args) {
Scanner leer = new Scanner(System.in);
n = leer.nextInt();
v = new int[n][n];
llenarForma();
mostrar();
} }
Corrida Python Corrida Java
4
1 5 6 7
8 2 9 10
11 12 3 13
14 15 16 4
59
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
Python Java
def llenarMar(): import java.util.Scanner;
c=1 public class ejer4 {
s=1 public static int v[][];
li = 0 public static int n;
ls = n public static void llenarMar() {
ms = 0 int i,j,c=1,s=1,li=0,ls=n;
if n % 2 == 0: for(i = 0; i < n; i++) {
ms = 1 for(j = li; j < ls; j++) {
for i in range(n): v[j][i] = c;
for j in range(li,ls): c = c + 1;
v[j][i] = c }
c=c+1 li = li + s;
li = li + s ls = ls - s;
ls = ls - s if(i == (n-1)/2) {
if li == n // 2: s = s * -1;
s = -1 if (n % 2 == 0) {
if n % 2 == 0: li = li + s;
li = li + s ls = ls - s;
ls = ls - s }
def mostrar(): } } }
for i in range(n): public static void mostrar() {
for j in range(n): int i,j;
print(v[i][j],end="\t") for(i = 0; i < n; i++) {
print() for(j = 0; j < n; j++) {
n = int(input()) System.out.print(v[i][j]+"\t");
v = [[0 for i in range(n)] for j in }
range(n)] System.out.println();
llenarMar() } }
mostrar() public static void main(String[] args) {
Scanner leer = new Scanner(System.in);
n = leer.nextInt();
v = new int[n][n];
llenarMar();
mostrar();
} }
Corrida Python Corrida Java
6
60
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
Ejercicio 5
Dado un numero N generar la
siguiente matriz para N x N.
Ejemplo:
N=5
1 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1
Df
Python Java
def llenarHip(): import java.util.Scanner;
c=1 public class ejer5 {
ls = n-1 public static int v[][];
for li in range(n//2): public static int n;
i = li public static void llenarMar() {
for j in range(li,ls): int i,j,c=1,s=1,li,ls=n-1;
v[i][j] = c for(li = 0; li < n/2; li++) {
j = ls i = li;
for i in range(li,ls): for(j = li; j < ls; j++) {
v[i][j] = c v[i][j] = c;
i = ls }
for j in range(ls,li-1,-1): j = ls;
v[i][j] = c for(i = li; i < ls; i++) {
j = li v[i][j] = c;
for i in range(ls-1,li,-1): }
v[i][j] = c i = ls;
c=c+1 for(j = ls; j > li-1; j--) {
ls = ls - 1 v[i][j] = c;
if n % 2 != 0: }
v[n//2][n//2] = c j = li;
def mostrar(): for(i = ls-1; i > li; i--) {
for i in range(n): v[i][j] = c;
for j in range(n): }
print(v[i][j],end="\t") c = c + 1;
print() ls = ls -1;
n = int(input()) }
v = [[0 for i in range(n)] for j in if(n % 2 != 0) {
range(n)] v[n/2][n/2] = c;
llenarHip() }
mostrar() }
public static void mostrar() {
int i,j;
61
MSc. Menfy Morales Aux. Roman Nina Aux. Carlos Jahuira INF111A-LAB11A-LAB111B 2/22
62