Este documento contiene la resolución de varios problemas de programación en Java. Presenta el código para generar histogramas, calcular productos de matrices, series de Fibonacci, números primos, ordenamiento y encriptación de mensajes usando el código César.
1. Problema 11: Imprimir un histograma de las edades del curso MA713 el numero de asteriscos corresponde con el
número de alumnos de las edades entre 15 y 25 años.
1 package problema11;
2 import java.util.Scanner;
3
4 public class Main
5 {
6 public static void main(String[] args)
7 {
8
9 Scanner s = new Scanner(System.in);
10 int []A = new int[11];
11 for(int i=0; i<11; i++)
12 A[i]=s.nextInt();
13 for(int i=0; i<11; i++)
14 {
15 System.out.print(i+15+" ");
16 for(int j=0; j<A[i]; j++)
17 System.out.print("*");
18 System.out.println();
19 }
20 }
21
22 }
23 run:
24 0
25 1
26 0
27 2
28 7
29 10
30 12
31 8
32 4
33 2
34 1
35 15
36 16 *
37 17
38 18 **
39 19 *******
40 20 **********
41 21 ************
42 22 ********
43 23 ****
44 24 **
45 25 *
46 GENERACIÓN CORRECTA (total time: 1 minute 8 seconds)
2. Problema 7: Cargar el arreglo A n*m, el arreglo Bm*p. Hallar el producto AxB
1 package problema7;
2 import java.util.Scanner;
3
4 public class Main
5 {
6 public static void main(String[] args)
7 {
8 int m,n,p;
9 Scanner s= new Scanner(System.in);
10 m = s.nextInt();
11 n = s.nextInt();
12 p = s.nextInt();
13 int [][]A = new int [m][n];
14 int [][]B = new int [n][p];
15 int [][]C = new int [m][p];
16 for(int i=0; i<m; i++)
17 for(int j=0; j<n; j++)
18 A[i][j]=s.nextInt();
19
20 for(int i=0; i<n; i++)
21 for(int j=0; j<p; j++)
22 B[i][j]=s.nextInt();
23
24 for(int i=0; i<m; i++)
25 for(int j=0; j<p; j++)
26 for(int k=0; k<n; k++)
27 C[i][j]+=A[i][k]*B[k][j];
28
29 System.out.println("El producto de la matriz AxB es: ");
30 for(int i=0; i<m; i++)
31 {
32 for(int j=0; j<p; j++)
33 System.out.print(C[i][j]+" ");
34 System.out.println();
35 }
36 }
37 }
38 run:
39 2 1 2
40 3
41 6
42 2
43 4
44 El producto de la matriz AxB es:
45 6 12
46 12 24
47 GENERACIÓN CORRECTA (total time: 8 seconds)
3. Problema 1: Imprimir la lista cargada con números de la serie Fibonacci, método visualizar() 0,1,1,2,3,5…
Leer la cantidad de números a sumar, usar método Fibo()
1 package problema1;
2
3 import java.util.Scanner;
4
5 public class Main
6 {
7 public static void main(String[] args)
8 {
9 // TODO code application logic here
10 int n;
11 Scanner Obj = new Scanner(System.in);
12 n = Obj.nextInt();
13 int []A = new int[100];
14 A[0]=0;
15 A[1]=1;
16 Fibo(n,A);
17 visualizar(A, n);
18 }
19
20 static void visualizar(int A[], int n){
21 for(int i=0; i<n-1; i++)
22 System.out.print(A[i]+", ");
23 System.out.println(A[n-1]);
24 }
25 static void Fibo(int n, int A[])
26 {
27 if(n<2)
28 return;
29 for(int i=2; i<n; i++)
30 A[i]=A[i-1]+A[i-2];
31 }
32 }
33 run:
34 6
35 0, 1, 1, 2, 3, 5
36 GENERACIÓN CORRECTA (total time: 6 seconds)
4. Problema 2: Determinar la suma y promedio de los n primeros números primos comprendidos entre 1 y 100
1 package problema2;
2
3 import java.util.Scanner;
4
5 public class Main
6 {
7 public static void main(String[] args)
8 {
9 Scanner Obj = new Scanner(System.in);
10 int n = Obj.nextInt();
11 int A[] = new int[100];
12 for(int i=2; i<Math.sqrt(100); i++)
13 {
14 int j=i+i;
15 while(j<100)
16 {
17 A[j]=1;
18 j+=i;
19 }
20 }
21
22 int i=0;
23 int j=2;
24 float suma=0;
25 float promedio=0;
26 while(i<n)
27 {
28 if(A[j]==0)
29 {
30 i++;
31 suma+=j;
32 }
33 j++;
34 }
35 promedio=suma/n;
36 System.out.println("La suma de los "+n+" primeros primos es "+suma);
37 System.out.println("El promedio de los "+n+" primeros primos es "+promedio);
38 }
39 }
40 run:
41 5
42 La suma de los 5 primeros primos es 28.0
43 El promedio de los 5 primeros primos es 5.6
44 GENERACIÓN CORRECTA (total time: 4 seconds)
5. Problema 3: Se tiene 2 listas An y Bm ordenados con datos, obtener la lista Cn+m combinando los elementos de A y B
1 package problema3;
2
3 import java.util.Scanner;
4
5 public class Main
6 {
7 public static void main(String[] args)
8 {
9 Scanner Obj = new Scanner(System.in);
10 int n = Obj.nextInt();
11 int []A = new int [n];
12 for(int i=0; i<n ; i++)
13 A[i] = Obj.nextInt();
14
15 int m = Obj.nextInt();
16 int []B = new int [m];
17 for(int i=0; i<m ; i++)
18 B[i] = Obj.nextInt();
19
20 int []C = new int [n+m];
21 int i=0,j=0;
22 for(int k=0; k<n+m; k++)
23 {
24 if(i>=n)
25 C[k]=B[j++];
26 else if(j>=m)
27 C[k]=A[i++];
28 else{
29 if(A[i]<B[j])
30 C[k]=A[i++];
31 else
32 C[k]=B[j++];
33 }
34 }
35 for(int k=0; k<n+m; k++)
36 System.out.print(C[k]+" ");
37 }
38 }
39 run:
40 4
41 1 2 3 4
42 5
43 5 6 7 8 9
44 1 2 3 4 5 6 7 8 9 GENERACIÓN CORRECTA (total time: 10 seconds)
6. Problema 5: Leer un mensaje y determinar el numero de palabras que tiene, cuantas letras y la palabra de mayor longitud
1 package problema5;
2 import java.util.Scanner;
3 public class Main
4 {
5 public static void main(String[] args)
6 {
7 Scanner Obj = new Scanner(System.in);
8 String msg = Obj.nextLine(); //String []palabras = msg.split(" ");
9 String []palabras = split(msg);
10
11 System.out.println("La frase tiene "+palabras.length+" palabras");
12 int mayor=palabras[0].length();
13 for(int i=1; i<palabras.length; i++)
14 if(mayor<palabras[i].length())
15 mayor=palabras[i].length();
16 System.out.println("La palabra de mayor longitud tiene "+mayor+" letras");
17 }
18 static String [] split(String msg){
19 int n=0;
20 for(int i=0; i<msg.length(); i++){
21 if(msg.charAt(i)==' ')
22 n++;
23 }
24 String[] words = new String[n+1];
25 //String[] words = new String[100];
26 int a=0;
27 int k=0;
28 for(int i=0; i<msg.length(); i++){
29 if(msg.charAt(i)==' '){
30 words[k++]=msg.substring(a, i);
31 a=i+1;
32 }
33 }
34 words[k]=msg.substring(a, msg.length());
35 return words;
36 }
37 }
38 run:
39 Hola como estas
40 La frase tiene 3 palabras
41 La palabra de mayor longitud tiene 5 letras
42 GENERACIÓN CORRECTA (total time: 10 seconds)
7. Problema 6: Leer un arreglo de n números enteros entre 500 y 1500 ordenar de mayor a menor, utilizando el algoritmo de
quicksort
1 package problema6;
2
3 import java.util.Scanner;
4
5 public class Main
6 {
7 public static void main(String[] args)
8 {
9 Scanner Obj = new Scanner(System.in);
10 int n = Obj.nextInt();
11 int []A = new int [n];
12 for(int i=0; i<n; i++)
13 A[i]=Obj.nextInt();
14
15 quicksort(A,0,n-1);
16 for(int i=0; i<n; i++)
17 System.out.print(A[i]+" ");
18 }
19
20 static void quicksort(int[] A, int i, int j) {
21
22 if(i==j)
23 return;
24
25 int r=particion(A,i,j);
26 if(i==r )//|| r==i+1)
27 {
28 quicksort(A,r+1,j);
29 }
30 else if(j==r )//|| r==j-1)
31 {
32 quicksort(A,i,r-1);
33 }
34 else
35 {
36 quicksort(A,i,r-1);
37 quicksort(A,r+1,j);
38 }
39
40
41 }
42 static int particion(int []A, int i, int j ){
43 /*if(i<j)
44 return i;*/
45 int pivot=A[i];
46 int r = i;