Vous êtes sur la page 1sur 21

TRABAJO PARA LA 4TA PRCTICA CALIFICADA

Codifique el mtodo recursivo para la busquedaBinaria(), que tiene como parmetro un


vector y el dato a buscar, si el dato est en el arreglo debe determinar la posicin del dato
en el vector si no esta debe retornar -1.

1 package javaapplication308;
2
3
4 public class JavaApplication308 {
5 public static int busquedaBinaria(int vector[], int dato){
6 int n = vector.length;
7 int centro,inf=0,sup=n-1;
8 while(inf<=sup){
9 centro=(sup+inf)/2;
10 if(vector[centro]==dato)
11 return centro;
12 else if(dato < vector [centro] ){
13 sup=centro-1; }
14 else {
15 inf=centro+1;
16 }
17 }
18 return -1;
19 }
20
21 public static void main(String[] args) {
22 int[]vector ={1,4,7,8,9,14,23,47,56,60,61,63,65,82};
23 int valorBuscado = 82;
24
25 System.out.print("Esta en la posicion:");
26 System.out.println(busquedaBinaria(vector,valorBuscado));
27
28 }
29
30 }
31 /*run:
32 Esta en la posicion:13
33 BUILD SUCCESSFUL (total time: 0 seconds)
1 package javaapplication308;
2
3
4 public class JavaApplication308 {
5 public static int busquedaBinaria(int vector[], int dato){
6 int n = vector.length;
7 int centro,inf=0,sup=n-1;
8 while(inf<=sup){
9 centro=(sup+inf)/2;
10 if(vector[centro]==dato)
11 return centro;
12 else if(dato < vector [centro] ){
13 sup=centro-1; }
14 else {
15 inf=centro+1;
16 }
17 }
18 return -1;
19 }
20
21 public static void main(String[] args) {
22 int[]vector ={1,4,7,8,9,14,23,47,56,60,61,63,65,82};
23 int valorBuscado = 2;
24
25 System.out.print("Esta en la posicion:");
26 System.out.println(busquedaBinaria(vector,valorBuscado));
27
28 }
29
30 }
31 /*run:
32 Esta en la posicion:-1
33 BUILD SUCCESSFUL (total time: 0 seconds)
Codifique el mtodo que imprima el histograma de frecuencia de 20 datos de nmeros
enteros comprendidos entre 0 y 9 generados aleatoriamente , la frecuencia de los datos
deben imprimirse con asteriscos.

1 package javaapplication16;
2 import java.util.Scanner;
3
4 public class HISTOGRAMA {
5
6
7 public static void generarS(int []V){
8 for( int i=0; i<V.length;i++){
9 V[i]= (int )(Math.random()*10);
10
11 }
12 }
13 public static void histogramaS(int []V){
14 int A []= new int[V.length];
15 for (int i=0;i<V.length;i++){
16 int cantidad =V[i];
17 for(int j=0;j<V.length;j++){
18 if( V[i]==V[j]){
19 cantidad++;
20 }
21 }
22 if(A[i]==0){
23 System.out.println(V[i]+":");
24 for(int k=0;k<cantidad;k++ ){
25 System.out.print("*");
26 }
27 System.out.println();
28 }
29 for(int j=0;j<V.length;j++){
30 if(V[i]==V[j]){
31 A[j]++;
32 }
33
34 }
35 }
36 }
37
38 public static void main(String[] args) {
39 Scanner obj = new Scanner(System.in);
40 System.out.print("Cantidad de elementos: ");
41 int n = obj.nextInt();
42 int[] V = new int[n];
43 generarS(V);
44 histogramaS(V);
45
46 }
47 }
48 /*run:
49 Cantidad de elementos: 6
50 6:00
51 ******
52 3:00
53 ***
54 9:00
55 *********
56 8:00
57 ********
58 7:00
59 *******
60 BUILD SUCCESSFUL (total time: 1 second)
Es un juego oriental que consta de tres columnas llamadas origen, destino y auxiliar y una
serie de discos de distintos tamaos. Los discos estn colocados de mayor a menor
tamao en la columna origen. El juego consiste en pasar todos los discos a la columna
destino y dejarlos como estaban de mayor a menor. (el ms grande en la base, el ms
pequeo arriba)
Las reglas del juego son las siguientes:
Slo se puede mover un disco cada vez.
Para cambiar los discos de lugar se pueden usar las tres columnas.
Nunca deber quedar un disco grande sobre un disco pequeo.

1 package javaapplication17;
2 import java.util.*;
3
4 public class Hanoi {
5 public static void Hanoi(int n, int origen, int auxiliar, int destino){
6 if(n==1)
7 System.out.println("mover disco de " + origen + " a " + destino);
8 else{
9 Hanoi(n-1, origen, destino, auxiliar);
10 System.out.println("mover disco de "+ origen + " a " + destino);
11 Hanoi(n-1, auxiliar, origen, destino);
12 }
13 }
14
15 public static void main(String[] args) {
16
17 Scanner sc = new Scanner(System.in);
18 int n;
19 System.out.println("Numero de discos: ");
20 n = sc.nextInt();
21 Hanoi(n,1,2,3);
22
23
24 }
25
26 }
27 /*run:
28 Numero de discos:
29 4
30 mover disco de 1 a 2
31 mover disco de 1 a 3
32 mover disco de 2 a 3
33 mover disco de 1 a 2
34 mover disco de 3 a 1
35 mover disco de 3 a 2
36 mover disco de 1 a 2
37 mover disco de 1 a 3
38 mover disco de 2 a 3
39 mover disco de 2 a 1
40 mover disco de 3 a 1
41 mover disco de 2 a 3
42 mover disco de 1 a 2
43 mover disco de 1 a 3
44 mover disco de 2 a 3
45 BUILD SUCCESSFUL (total time: 2 seconds)*/
TRABAJO PARA LA TERCERA PRCTICA
CALIFICADA
Dado el Sistema de ecuacin hallar la solucin utilizando el algoritmo de sustitucin
regresiva.

1 package javaapplication14;
2
3 public class MA713 {
4 public static double[][] cargarmatriz(double[][]A){
5 for(int i=0;i<A.length;i++){
6 for(int j=0;j<A[i].length;j++){
7 A[i][j]=Math.random()*21;
8 System.out.printf("%10f",A[i][j]);
9 }
10 System.out.println();
11 }
12 return A;
13 }
14 public static double[][] matriz(double[][]A){
15 for(int i=0;i<A.length;i++){
16 double m=A[i][i];
17 for(int j=0;j<A[i].length;j++){
18 A[i][j]=A[i][j]/m;
19 System.out.printf("%10f",A[i][j]);
20 }
21 System.out.println();
22 for(int k=i+1;k<A.length;k++){
23 double p=A[k][i];
24 for(int j=0;j<A[0].length;j++)
25 A[k][j]=A[k][j]-p*A[i][j];
26 }
27 }
28 return A;
29 }
30
31
32 public static void main(String[] args) {
33 double[][]A=new double[4][4];
34 System.out.println("Matriz original:");
35 A=cargarmatriz(A);
36 System.out.println("Matriz triangular:");
37 A=MA713.matriz(A);
38
39 }
40
41 }
42
43 /*Matriz original:
44 5.693030 13.940870 12.890666 13.833930
45 2.811889 13.785891 13.556644 11.877100
46 17.155051 13.231959 16.442311 16.602298
47 7.243373 6.442408 19.578406 1.891682
48 Matriz triangular:
49 1.000000 2.448761 2.264289 2.429977
50 0.000000 1.000000 1.041950 0.731028
51 0.000000 0.000000 1.000000 -0.533822
52 0.000000 0.000000 0.000000 1.000000*/
Imprimir la matriz triangular superior

1 package javaapplication14;
2
3 public class MA713 {
4 public static double[][] diagonal(double[][]A){
5 for(int i=0;i<A.length;i++){
6 for(int j=0;j<A.length;j++){
7 if(j>=i){
8 A[i][j]=Math.random()*21;
9 }
10
11 System.out.printf("%10f",A[i][j]);
12 }
13 System.out.println();
14 }
15 return A;
16 }
17
18 public static void main(String[] args) {
19 double[][]A=new double[4][4];
20
21 System.out.println("Matriz triangular:");
22 A=MA713.diagonal(A);
23
24 }
25
26 }
27
28 /*run:
29 Matriz triangular:
30 7.571565 10.262005 15.024237 20.570258
31 0.000000 2.541500 9.869081 3.465375
32 0.000000 0.000000 0.346714 3.038448
33 0.000000 0.000000 0.000000 6.640434
34 BUILD SUCCESSFUL (total time: 0 seconds)*/
TRABAJO PARA LA SEGUNDA PRCTICA
CALIFICADA

Cdigo para determinar si un nmero ingresado por teclado es primo

1 package javaapplication3;
2 import java.util.Scanner;
3
4 public class JavaApplication3 {
5
6 public static void main(String[] args) {
7
8 Scanner in=new Scanner(System.in);
9 System.out.println("Introduce numero primo: ");
10 int i=2, n=in.nextInt();
11 while(n%i!=0)i++;
12 if (i==n) System.out.println(" Si es Primo");
13 else System.out.println("NO Primo");
14 }
15 }
16
17
18 /*run:
19 Introduce numero primo:
20 4
21 NO Primo
22 BUILD SUCCESSFUL (total time: 7 seconds)*/
23
24
25
26 /*run:
27 Introduce numero primo:
28 7
29 Si es Primo
30 BUILD SUCCESSFUL (total time: 1 second)*/
Cdigo para determinar el rea de un tringulo en funcin del semiperimetro y sus lados

1 package javaapplication3;
2
3 import java.util.Scanner;
4 public class JavaApplication3 {
5 public static void main(String[] args) {
6 Scanner sc = new Scanner(System.in);
7 double a,b,c;
8
9 System.out.println("Ingrese el lado a");
10 a=sc.nextDouble();
11 System.out.println("Ingresa el lado b");
12 b=sc.nextDouble();
13 System.out.println("Ingresa el lado c");
14 c=sc.nextDouble();
15 double p,area;
16 p=(a+b+c)/2;
17 area=Math.sqrt(p*(p-a)*(p-b)*(p-c));
18 System.out.println("El area es " + area);
19 }
20 }
21
22
23 /*run:
24 Ingrese el lado a
25 3
26 Ingresa el lado b
27 4
28 Ingresa el lado c
29 5
30 El area es 6.0
31 BUILD SUCCESSFUL (total time: 2 seconds)*/
Cdigo para determinar si un nmero es capica

1 package javaapplication1;
2
3
4 import java.io.*;
5 public class JavaApplication1
6 {
7 public static void capi (int n)
8 {
9 int r,s=0,k;
10 k=n;
11 while(n!=0)
12 {
13 r=n%10;
14 s=s*10+r;
15 n=n/10;
16 }
17
18 if(s==k)
19 System.out.println ("\nEs capicua ");
20 else
21 System.out.println ("\nNo es capicua ");
22 }
23
24 public static void main (String orgs[])throws IOException
25 {
26 int n;
27 BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
28 System.out.println("Introducir numero decimal entero:");
29 n=Integer.parseInt(in.readLine());
30 capi(n);
31 }
32 }
33
34 /*run:
35 Introducir numero decimal entero:
36 1441
37
38 Es capicua
39 BUILD SUCCESSFUL (total time: 4 seconds)*/
40
41
42 /*run:
43 Introducir numero decimal entero:
44 159
45
46 No es capicua
47 BUILD SUCCESSFUL (total time: 4 seconds)*/
TRABAJO PARA LA PRIMERA PRCTICA
CALIFICADA

Vous aimerez peut-être aussi