You are on page 1of 12

Generar 5,10,15,20,25,30,...

Cdigo:
class JavaSeries1
{
public static void main (String args [])
{
int n, c = 1, serie = 5;
System.out.print ("Cantidad d terminos: ");
n = Leer.datoInt ();
while (c <= n)
{
System.out.print ("," + serie);
serie += 5;
c++;
}
}
}

Si n=7 generar 7,6,5,4,3,2,1


Cdigo:
class JavaSeries2
{
public static void main (String args [])
{
int n, c = 1;
System.out.print ("Cantidad d terminos: ");
n = Leer.datoInt ();
int serie = n;
while (c <= n)
{
System.out.print (serie + ",");
serie--;
c++;
}
}
}

>> VECTORES <<


/*Dado el vector T de tamao n. Si el tamao es par invertir los elementos de la mitad de los elementos
Ejemplo: v=[1][2][3][4][5][6] v(invertido)=[3][2][1][6][5][4]
*/
Cdigo:
class JavaVectores1
{
void llenar (int V [], int d)
{
for (int i = 1 ; i <= d ; i++)
{
System.out.print ("Inserte pos.[" + i + "]: ");
V [i] = Leer.datoInt ();
}
}

void mostrar (int V [], int d)


{
for (int i = 1 ; i <= d ; i++)
{
System.out.print ("[" + V [i] + "]");
}
}

void invierte (int V [], int d)


{
int aux1;
int fin1 = d / 2;
for (int i = 1 ; i <= (d / 2) / 2 ; i++)
{
aux1 = V [i];
V [i] = V [fin1];
V [fin1] = aux1;
fin1--;
}

fin1 = d;
for (int j = (d / 2) + 1 ; j <= (d / 2) + 1 ; j++)
{
aux1 = V [j];
V [j] = V [fin1];
V [fin1] = aux1;
fin1--;
}
}

public static void main (String args [])


{
JavaVectores1 h = new JavaVectores1 ();
int V [] = new int [20];
System.out.print ("Inserte dimen. del vector: ");
int d = Leer.datoInt ();
h.llenar (V, d);
System.out.println ("\nVECTOR ORIGINAL: ");
h.mostrar (V, d);
System.out.println ("\nVECTOR LUEGO DE LA INVERSION: ");
h.invierte (V, d);
h.mostrar (V, d);
}
}

/*Dado un polinomio evualuarlo en el punto x (todo en un vector)*/


Cdigo:
class JavaVectores2
{
void llenar (int V [], int d)
{
for (int i = 1 ; i <= d ; i++)
{
System.out.print ("Inserte pos.[" + i + "]: ");
V [i] = Leer.datoInt ();
}
}

void mostrar (int V [], int d)


{
for (int i = 1 ; i <= d ; i++)
{
System.out.print ("[" + V [i] + "]");
}
}

int potencia (int b, int e)


{
int p = 1;
for (int i = 1 ; i <= e ; i++)
{
p = p * b;
}
return (p);
}

void evalua (int V [], int d, int x)


{
int s = 0;
for (int i = 1 ; i <= d ; i += 2)
{
s = s + (V [i] * potencia (x, V [i + 1]));
}
System.out.println ("\n\nX es igual a: " + s);
}

public static void main (String args [])


{
JavaVectores2 h = new JavaVectores2 ();
int V [] = new int [20];
System.out.print ("Inserte dimen. del vector: ");
int d = Leer.datoInt ();
System.out.print ("Inserte valor de (x): ");
int x = Leer.datoInt ();
h.llenar (V, d);
System.out.println ("\nVECTOR: ");
h.mostrar (V, d);
h.evalua (V, d, x);
}
}

>> MATRICES <<


/*Dadas dos matrices A y B intercambiar los minimos de A con los maximos de B*/
Cdigo:
class JavaMatrices1
{
void llenar (int M [] [], int f, int c)
{
for (int i = 1 ; i <= f ; i++)
{
for (int j = 1 ; j <= c ; j++)
{
System.out.print ("Inserte pos[" + i + "][" + j + "]: ");
M [i] [j] = Leer.datoInt ();
}
}
}

void mostrar (int M [] [], int f, int c)


{
for (int i = 1 ; i <= f ; i++)
{
System.out.println ();
for (int j = 1 ; j <= c ; j++)
{
System.out.print ("[" + M [i] [j] + "]");
}
}
}

int menor (int M [] [], int f, int c)


{
int men = M [1] [1];
for (int i = 1 ; i <= f ; i++)
{
for (int j = 1 ; j <= c ; j++)
{
if (M [i] [j] < men)
men = M [i] [j];
}
}
return (men);
}

int maximo (int M [] [], int f, int c)


{
int max = M [1] [1];
for (int i = 1 ; i <= f ; i++)
{
for (int j = 1 ; j <= c ; j++)
{
if (M [i] [j] < max)
max = M [i] [j];
}
}
return (max);
}

void intercambiar (int A [] [], int fa, int ca, int B [] [], int fb, int cb)
{
int min_a = menor (A, fa, ca);
int max_b = maximo (B, fb, cb);

//para cambiar los minimos de A con los maximos de B


for (int i = 1 ; i <= fa ; i++)
{
for (int j = 1 ; j <= ca ; j++)
{
if (A [i] [j] == min_a)
A [i] [j] = max_b;
}
}

//para intercambiar los maximos de con los minimos de A


for (int i = 1 ; i <= fb ; i++)
{
for (int j = 1 ; j <= cb ; j++)
{
if (B [i] [j] == max_b)
B [i] [j] = min_a;
}
}
}

public static void main (String args [])


{
JavaMatrices1 h = new JavaMatrices1 ();
int A [] [] = new int [20] [20];
int B [] [] = new int [20] [20];
System.out.print ("Insert filas de A: ");
int fa = Leer.datoInt ();
System.out.print ("Insert columnas de A: ");
int ca = Leer.datoInt ();
System.out.print ("Insert filas de B: ");
int fb = Leer.datoInt ();
System.out.print ("Insert columnas de B: ");
int cb = Leer.datoInt ();

//lectura de matrices
System.out.println ("\nINSERTANDO DATOS EN MATRIS A: \n");
h.llenar (A, fa, ca);
System.out.println ("\nINSERTANDO DATOS EN MATRIS B: \n");
h.llenar (B, fb, cb);
System.out.println ("\nMATRICES ORIGINALMENTE INSERTADAS: ");
h.mostrar (A, fa, ca);
System.out.println ();
h.mostrar (B, fb, cb);
System.out.println ();

//intercambiando elementos
h.intercambiar (A, fa, ca, B, fb, cb);
System.out.println ("\nMATRICES DESPUES DEL INTERCAMBIO:");
h.mostrar (A, fa, ca);
System.out.println ();
h.mostrar (B, fb, cb);
}
}

/*Dada una matris cuadrada invertir su diagonal principal*/


Cdigo:
class JavaMatrices2
{
void llenar (int M [] [], int d)
{
for (int i = 1 ; i <= d ; i++)
{
for (int j = 1 ; j <= d ; j++)
{
System.out.print ("Inserte pos[" + i + "][" + j + "]: ");
M [i] [j] = Leer.datoInt ();
}
}
}

void mostrar (int M [] [], int d)


{
for (int i = 1 ; i <= d ; i++)
{
System.out.println ();
for (int j = 1 ; j <= d ; j++)
{
System.out.print ("[" + M [i] [j] + "]");
}
}
}

void invierte (int M [] [], int d)


{
int fin = d;
for (int i = 1 ; i <= d / 2 ; i++)
{
int aux = M [i] [i];
M [i] [i] = M [d] [d];
M [d] [d] = aux;
fin--;
}
}

public static void main (String args [])


{
JavaMatrices2 h = new JavaMatrices2 ();
int M [] [] = new int [20] [20];
System.out.print ("Inserte dimen. de la matris cuadrada: ");
int d = Leer.datoInt ();
h.llenar (M, d);
System.out.print ("\nMATRIS ORIGINAL: ");
h.mostrar (M, d);
System.out.print ("\n\nMATRIS CON LA DIAGONAL PRINCIPAL INVERTIDA: ");
h.invierte (M, d);
h.mostrar (M, d);
}
}

/*Dada una matris cuadrada invertir su diagonal secundaria*/


Cdigo:
class JavaMatrices3
{
void llenar (int M [] [], int d)
{
for (int i = 1 ; i <= d ; i++)
{
for (int j = 1 ; j <= d ; j++)
{
System.out.print ("Inserte pos[" + i + "][" + j + "]: ");
M [i] [j] = Leer.datoInt ();
}
}
}

void mostrar (int M [] [], int d)


{
for (int i = 1 ; i <= d ; i++)
{
System.out.println ();
for (int j = 1 ; j <= d ; j++)
{
System.out.print ("[" + M [i] [j] + "]");
}
}
}

void invierte (int M [] [], int d)


{
int fin = d;
for (int i = 1 ; i <= d / 2 ; i++)
{
int aux = M [i] [d];
M [i] [d] = M [d] [i];
M [d] [i] = aux;
fin--;
}
}

public static void main (String args [])


{
JavaMatrices3 h = new JavaMatrices3 ();
int M [] [] = new int [20] [20];
System.out.print ("Inserte dimen. de la matris cuadrada: ");
int d = Leer.datoInt ();
h.llenar (M, d);
System.out.print ("\nMATRIS ORIGINAL: ");
h.mostrar (M, d);
System.out.print ("\n\nMATRIS CON LA DIAGONAL SECUNDARIA INVERTIDA: ");
h.invierte (M, d);
h.mostrar (M, d);
}
}

/*Dada dos matrices de diferentes tamanios R y S mostrar los elementos comunes de R en S*/
Cdigo:
class JavaMatrices4
{
void llenar (int M [] [], int f, int c)
{
for (int i = 1 ; i <= f ; i++)
{
for (int j = 1 ; j <= c ; j++)
{
System.out.print ("Inserte pos[" + i + "][" + j + "]: ");
M [i] [j] = Leer.datoInt ();
}
}
}
void mostrar (int M [] [], int f, int c)
{
for (int i = 1 ; i <= f ; i++)
{
System.out.println ();
for (int j = 1 ; j <= c ; j++)
{
System.out.print ("[" + M [i] [j] + "]");
}
}
}

void comunes (int R [] [], int fr, int cr, int S [] [], int fs, int cs)
{
System.out.print ("\n\nLos elementos comunes de R en S son: ");
for (int i = 1 ; i <= fr ; i++)
{
for (int j = 1 ; j <= cr ; j++)
{
for (int k = 1 ; k <= fs ; k++)
{
for (int l = 1 ; l <= cs ; l++)
{
if (R [i] [j] == S [k] [l])
System.out.print ("[" + R [i] [j] + "]");
}
}
}
}
}

public static void main (String args [])


{
JavaMatrices4 h = new JavaMatrices4 ();
int R [] [] = new int [20] [20];
int S [] [] = new int [20] [20];
System.out.print ("Inserte filas de R: ");
int fr = Leer.datoInt ();
System.out.print ("Inserte columnas de R: ");
int cr = Leer.datoInt ();
System.out.print ("Inserte filas de S: ");
int fs = Leer.datoInt ();
System.out.print ("Inserte columnas de S: ");
int cs = Leer.datoInt ();

System.out.print ("\nLLENANDO MATRIS R: \n");


h.llenar (R, fr, cr);
System.out.print ("\nLLENANDO MATRIS S: \n");
h.llenar (S, fs, cs);
System.out.print ("\nLA MATRICES R : ");
h.mostrar (R, fr, cr);
System.out.print ("\nLA MATRICES S : ");
h.mostrar (S, fs, cs);
h.comunes (R, fr, cr, S, fs, cs);
}
}

/*Dada una matris intercambiar los elementos de la primera columna con la ultima columna*/
Cdigo:
class JavaMatrices5
{
void llenar (int M [] [], int f, int c)
{
for (int i = 1 ; i <= f ; i++)
{
for (int j = 1 ; j <= c ; j++)
{
System.out.print ("Inserte pos[" + i + "][" + j + "]: ");
M [i] [j] = Leer.datoInt ();
}
}
}

void mostrar (int M [] [], int f, int c)


{
for (int i = 1 ; i <= f ; i++)
{
System.out.println ();
for (int j = 1 ; j <= c ; j++)
{
System.out.print ("[" + M [i] [j] + "]");
}
}
}

void intercambiar (int M [] [], int f, int c)


{
for (int i = 1 ; i <= f ; i++)
{
int aux = M [i] [1];
M [i] [1] = M [i] [c];
M [i] [c] = aux;
}
}

public static void main (String args [])


{
JavaMatrices5 h = new JavaMatrices5 ();
int M [] [] = new int [20] [20];
System.out.print ("Inserte filas de la matris: ");
int f = Leer.datoInt ();
System.out.print ("Inserte columnas de la matris: ");
int c = Leer.datoInt ();

System.out.print ("\nLLENANDO MATRIS : \n");


h.llenar (M, f, c);
System.out.print ("\nLA MATRIS ORIGINAL : ");
h.mostrar (M, f, c);
System.out.print ("\n\nLA MATRICES INTERCAMBIADA : ");
h.intercambiar (M, f, c);
h.mostrar (M, f, c);
}
}

/* Contar el numero de digitos de cada elemento de una matris */


Cdigo:
class JavaMatrices6
{
void llenar (int M [] [], int f, int c)
{
for (int i = 1 ; i <= f ; i++)
{
for (int j = 1 ; j <= c ; j++)
{
System.out.print ("Inserte pos[" + i + "][" + j + "]: ");
M [i] [j] = Leer.datoInt ();
}
}
}

void mostrar (int M [] [], int f, int c)


{
for (int i = 1 ; i <= f ; i++)
{
System.out.println ();
for (int j = 1 ; j <= c ; j++)
{
System.out.print ("[" + M [i] [j] + "]");
}
}

void cuenta (int M [] [], int f, int c)


{
for (int i = 1 ; i <= f ; i++)
{
for (int j = 1 ; j <= c ; j++)
{
System.out.print ("\n[" + M [i] [j] + "] tiene: " + digitos (M [i] [j]) + " digito(s)");
}
}
}

int digitos (int n)


{
int contador = 0;
while (n != 0)
{
n = n / 10;
contador++;
}
return (contador);
}

public static void main (String args [])


{
JavaMatrices6 h = new JavaMatrices6 ();
int M [] [] = new int [20] [20];
System.out.print ("Inserte filas de la matris: ");
int f = Leer.datoInt ();
System.out.print ("Inserte columnas de la matris: ");
int c = Leer.datoInt ();

System.out.print ("\nLLENANDO MATRIS M: \n");


h.llenar (M, f, c);
System.out.print ("\nLA MATRIS: ");
h.mostrar (M, f, c);
System.out.print ("\n\nCONTEO DE DIGITOS: ");
h.cuenta (M, f, c);
}
}

Problema 1:
Crear una matriz de n * m filas (cargar n y m por teclado) Imprimir la matriz completa y la ltima fila.

Programa:

import java.util.Scanner;
public class Matriz5 {
private Scanner teclado;
private int[][] mat;
public void cargar() {
teclado=new Scanner(System.in);
System.out.print("Cuantas fila tiene la
matriz:");
int filas=teclado.nextInt();
System.out.print("Cuantas columnas tiene
la matriz:");
int columnas=teclado.nextInt();
mat=new int[filas][columnas];
for(int f=0;f<mat.length;f++) {
for(int c=0;c<mat[f].length;c++) {
System.out.print("Ingrese
componente:");
mat[f][c]=teclado.nextInt();
}
}
}

public void imprimir() {


for(int f=0;f<mat.length;f++) {
for(int c=0;c<mat[f].length;c++) {
System.out.print(mat[f][c]+" ");
}
System.out.println();
}
}

public void imprimirUltimaFila() {


System.out.println("Ultima fila");
for(int c=0;c<mat[mat.length-
1].length;c++) {
System.out.print(mat[mat.length-
1][c]+" ");
}
}

public static void main(String[] ar) {


Matriz5 ma=new Matriz5();
ma.cargar();
ma.imprimir();
ma.imprimirUltimaFila();
}
}
En este ejemplo cada vez que se ejecute el programa el tamao de la matriz lo define el usuario, para ello ingresamos por teclado dos enteros y
seguidamente procedemos a crear la matriz con dichos valores:
System.out.print("Cuantas fila tiene la matriz:");
int filas=teclado.nextInt();
System.out.print("Cuantas columnas tiene la matriz:");
int columnas=teclado.nextInt();
mat=new int[filas][columnas];
Ahora las estructuras repetitivas las acotamos preguntando a la misma matriz la cantidad de filas y la cantidad de elementos de cada fila(mat.length
almacena la cantidad de filas de la matriz y mat[f].length cuando f vale cero accedemos a la cantidad de elementos de la fila cero y as sucesivamente
para cada valor de f):
for(int f=0;f<mat.length;f++) {
for(int c=0;c<mat[f].length;c++) {
System.out.print("Ingrese componente:");
mat[f][c]=teclado.nextInt();
}
}
El algoritmo de impresin es idntico al visto anteriormente con la modificacin de las condiciones de los for:
public void imprimir() {
for(int f=0;f<mat.length;f++) {
for(int c=0;c<mat[f].length;c++) {
System.out.print(mat[f][c]+" ");
}
System.out.println();
}
}
Para imprimir la ltima fila debemos disponer un valor fijo en el subndice de la fila (en este caso no podemos disponer un nmero fijo sino preguntarle a
la misma matriz la cantidad de filas y restarle uno ya que las filas comienzan a numerarse a partir de cero: mat[mat.length-1][c])
Tambin la condicin del for debemos acceder al atributo length de la ltima fila mat[mat.length-1].length
for(int c=0;c<mat[mat.length-1].length;c++) {
System.out.print(mat[mat.length-1][c]+" ");
}

Problema 2:
Crear una matriz de n * m filas (cargar n y m por teclado) Imprimir el mayor elemento y la fila y columna donde se almacena.

Programa:

import java.util.Scanner;
public class Matriz6 {
private Scanner teclado;
private int[][] mat;

public void cargar() {


teclado=new Scanner(System.in);
System.out.print("Cuantas fila tiene la
matriz:");
int filas=teclado.nextInt();
System.out.print("Cuantas columnas tiene
la matriz:");
int columnas=teclado.nextInt();
mat=new int[filas][columnas];
for(int f=0;f<mat.length;f++) {
for(int c=0;c<mat[f].length;c++) {
System.out.print("Ingrese
componente:");
mat[f][c]=teclado.nextInt();
}
}
}

public void imprimirMayor() {


int mayor=mat[0][0];
int filamay=0;
int columnamay=0;
for(int f=0;f<mat.length;f++) {
for(int c=0;c<mat[f].length;c++) {
if (mat[f][c]>mayor) {
mayor=mat[f][c];
filamay=f;
columnamay=c;
}
}
}
System.out.println("El elemento mayor
es:"+mayor);
System.out.println("Se encuentra en la
fila:"+filamay+ " y en la columna: "+columnamay);
}

public static void main(String[] ar) {


Matriz6 ma=new Matriz6();
ma.cargar();
ma.imprimirMayor();
}
}
Para obtener el mayor elemento de la matriz y la fila y columna donde se ubica debemos inicializar una variable mayor con el elemento de la fila cero y
columna cero (esto lo hacemos suponiendo que en dicha posicin se almacena el mayor):
int mayor=mat[0][0];
int filamay=0;
int columnamay=0;
Luego mediante dos for recorremos todos los elementos de la matriz y cada vez que encontramos un elemento mayor al actual procedemos a actualizar
la variable mayor y la posicin donde se almacena:
for(int f=0;f<mat.length;f++) {
for(int c=0;c<mat[f].length;c++) {
if (mat[f][c]>mayor) {
mayor=mat[f][c];
filamay=f;
columnamay=c;
}
}
}
Problemas propuestos

1. Crear una matriz de n * m filas (cargar n y m por teclado) Intercambiar la primer fila con la segundo. Imprimir luego la matriz.
2. Crear una matriz de n * m filas (cargar n y m por teclado) Imprimir los cuatro valores que se encuentran en los vrtices de la misma
(mat[0][0] etc.)

import java.util.Scanner;
public class Matriz7 {
private Scanner teclado;
private int[][] mat;

public void cargar() {


teclado=new Scanner(System.in);
System.out.print("Cuantas fila tiene la matriz:");
int filas=teclado.nextInt();
System.out.print("Cuantas columnas tiene la
matriz:");
int columnas=teclado.nextInt();
mat=new int[filas][columnas];
for(int f=0;f<mat.length;f++) {
for(int c=0;c<mat[f].length;c++) {
System.out.print("Ingrese componente:");
mat[f][c]=teclado.nextInt();
}
}
}

public void intercambiar() {


for(int c=0;c<mat[0].length;c++) {
int aux=mat[0][c];
mat[0][c]=mat[1][c];
mat[1][c]=aux;
}
}

public void imprimir() {


for(int f=0;f<mat.length;f++) {
for(int c=0;c<mat[f].length;c++) {
System.out.print(mat[f][c]+" ");
}
System.out.println();
}
}

public static void main(String[] ar) {


Matriz7 ma=new Matriz7();
ma.cargar();
ma.intercambiar();
ma.imprimir();
}
}

import java.util.Scanner;
public class Matriz8 {
private Scanner teclado;
private int[][] mat;

public void cargar() {


teclado=new Scanner(System.in);
System.out.print("Cuantas fila tiene la matriz:");
int filas=teclado.nextInt();
System.out.print("Cuantas columnas tiene la
matriz:");
int columnas=teclado.nextInt();
mat=new int[filas][columnas];
for(int f=0;f<mat.length;f++) {
for(int c=0;c<mat[f].length;c++) {
System.out.print("Ingrese componente:");
mat[f][c]=teclado.nextInt();
}
}
}

public void imprimirVertices() {


System.out.println("Vrtice superior izquierdo:");
System.out.println(mat[0][0]);
System.out.println("Vrtice superior derecho:");
System.out.println(mat[0][mat[0].length-1]);
System.out.println("Vrtice inferior izquierdo:");
System.out.println(mat[mat.length-1][0]);
System.out.println("Vrtice inferior derecho:");
System.out.println(mat[mat.length-1][mat[mat.length-
1].length-1]);
}

public static void main(String[] ar) {


Matriz8 ma=new Matriz8();
ma.cargar();
ma.imprimirVertices();
}
}

You might also like