Professional Documents
Culture Documents
RECURSIVIDAD
ejercicio
analizar el siguiente
Module Module2
Public Const maxfilas As Integer = 3500
Public Const maxcol As Integer = 100
Public contador As Integer, tcombi As Integer
Public ne As Integer = 4
Public B(maxcol) As Integer
Public B1(maxcol) As Integer
Public A(maxfilas, maxcol) As Integer
Function Diferentes(ByVal VA() As Integer,
ByVal ne As Integer) As Boolean
Dim fila As Integer, col As Integer
Dim valor As Boolean = True
For fila = 0 To ne - 1
For col = fila + 1 To ne
If VA(fila) = VA(col) Then
valor = False
Exit For
End If
Next
Next
Diferentes = valor
End Function
Sub Imprimirvector(ByVal VA() As Integer, ByVal nf As Integer)
Dim fila As Integer
For fila = 0 To nf
Console.Write(" {0} ", VA(fila).ToString.PadLeft(5))
Next
End Sub
Sub Permutaciones(ByRef A(,) As Integer, ByRef B() As Integer, ByVal B1()
As Integer, ByVal n As Integer, ByVal ne As Integer)
Dim I As Integer, k As Integer, valor
If n > 0 Then
For k = 0 To ne
B(n) = B1(k)
permutaciones(A, B, B1, n - 1, ne)
Next k
Else
For k = 0 To ne
B(n) = B1(k)
valor = Diferentes(B, ne)
If valor = True Then
For I = 0 To ne
A(contador, I) = B(I)
Next I
contador = contador + 1
End If
Next k
End If
End Sub
Sub PermutacionesConRepeticiones(ByRef A(,) As Integer, ByRef B() As
Integer, ByVal B1() As Integer, ByVal n As Integer, ByVal ne As Integer)
Dim I As Integer, k As Integer
If n > 0 Then
For k = 0 To ne
B(n) = B1(k)
PermutacionesConRepeticiones(A, B, B1, n - 1, ne)
Next k
Else
For k = 0 To ne
B(n) = B1(k)
For I = 0 To ne
A(contador, I) = B(I)
Next I
contador = contador + 1
Next k
End If
End Sub
Sub IniciarMatriz(ByVal A(,) As Integer, ByVal nf As Integer, ByVal nc As
Integer)
Dim fila As Integer, col As Integer
For fila = 0 To nf - 1
For col = 0 To nc - 1
A(fila, col) = 0
Next
Next
End Sub
Sub IniciarVector(ByVal V() As Integer, ByVal nf As Integer)
Dim fila As Integer
For fila = 0 To nf
V(fila) = fila
Next
End Sub
Sub ImprimirMatriz(ByVal A(,) As Integer, ByVal nf As Integer, ByVal nc As
Integer)
Dim fila As Integer, col As Integer
For fila = 0 To nf - 1
Console.WriteLine()
For col = 0 To nc - 1
Console.Write("{0} ", A(fila, col).ToString.PadLeft(5))
A(fila, col) = 0
Next
Next
End Sub
End Module
CODIGO DEL MODULO 1
Option Explicit On
Module Module1
Sub main()
contador = 0
ne = 3
IniciarVector(B1, ne + 1)
PermutacionesConRepeticiones(A, B, B1, ne - 1, ne - 1)
tcombi = contador
Console.WriteLine("PERMUTACIONES CON REPETICION")
ImprimirMatriz(A, tcombi, ne)
contador = 0
Console.WriteLine()
Console.WriteLine("PERMUTACIONES SIN REPETICION")
Permutaciones(A, B, B1, ne - 1, ne - 1)
tcombi = contador
ImprimirMatriz(A, tcombi, ne)
Console.ReadLine()
End Sub
End Module
Implemente el mismo problema como funciones personalizadas en Excel para
ello consulte el documento adjunto enviado
Paso 1 Clic en la ficha progrador y luego visual basic
Luego insertar modulo
Su estructura de proyecto debe quedar asi
If n > 0 Then
For k = 0 To ne
B(n) = B1(k)
Call PermutacionesConRepeticiones(A, B, B1, n - 1, ne)
Next k
Else
For k = 0 To ne
B(n) = B1(k)
For I = 0 To ne
A(contador, I) = B(I)
Next I
contador = contador + 1
Next k
End If
End Sub
Sub PermutacionesSR(B() As Integer, B1() As Integer, n As Integer, ne As
Integer)
Dim I As Integer, k As Integer, valor
If n > 0 Then
For k = 0 To ne
B(n) = B1(k)
Call PermutacionesSR(B, B1, n - 1, ne)
Next k
Else
For k = 0 To ne
B(n) = B1(k)
valor = Diferentes(B, ne)
If valor = True Then
For I = 0 To ne
A(contador, I) = B(I)
Next I
contador = contador + 1
End If
Next k
End If
End Sub
Con esto ya se implemnto las siguientes funciones definidas por el usuario
Para usarlos use la formula de las funciones
Y se tiene las siguientes funciones definidas
Use por ejemplo la funcion PermuSinRep que obtiene las permutaciones sin
repeticiones
Arreglar
Option Explicit On
Option Strict On
Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class Form1
Dim Pen As Pen
Dim Grafico As Graphics
Dim brocha As SolidBrush
Dim npuntos As Integer = 1000
Dim lx As Single = 3
Dim ly As Single = 0
Dim lambda As complexNum
Dim z0 As complexNum
Dim zx As Single = 1.5
Dim zy As Single = 0.4
'// Establace los limites de las coordenadas del plano complejo
Public xComplexMin As Single = -0.25 ' -200 * 4 '-2.0
Public xComplexMax As Single = 1.25 ' 50 * 8 ' 0.5
Public yComplexMin As Single = -0.75 '-125 * 4 '-1.25
Public yComplexMax As Single = 0.75 '125 * 4 '1.25
Public Ex As Single = 100
Public Ey As Single = 100
Public Cx As Single = 200
Public cy As Single = 200
Public Structure complexNum
Public x As Single
Public y As Single
End Structure
Sub solveQuadraticEq(ByVal lambda As complexNum, ByRef z As complexNum)
Dim lambdaMagSq, DscrMag As Single
End Sub
Imports System.Drawing
Public Class Form1
Dim Grafico1 As Graphics
Dim Grafico2 As Graphics
Dim Grafico3 As Graphics
Dim Grafico4 As Graphics
Dim Grafico5 As Graphics
PictureBox2.Width = ancho + cx
PictureBox2.Height = alto + cy
PictureBox3.Width = ancho + cx
PictureBox3.Height = alto + cy
PictureBox4.Width = ancho + cx
PictureBox4.Height = alto + cy
PictureBox5.Width = ancho + cx
PictureBox5.Height = alto + cy
PictureBox6.Width = ancho + cx
PictureBox6.Height = alto + cy
PictureBox7.Width = ancho + cx
PictureBox7.Height = alto + cy
Cuadro.Width = ancho * 2 + cx
Cuadro.Height = alto * 2 + cy
Pict1 = New Bitmap("e:\datos2\platano1.bmp")
Pict2 = New Bitmap("e:\datos2\platano2.bmp")
Pict3 = New Bitmap("e:\datos2\platano3.bmp")
Pict4 = New Bitmap("e:\datos2\platano4.bmp")
Pict5 = New Bitmap("e:\datos2\platano5.bmp")
Pict6 = New Bitmap("e:\datos2\platano6.bmp")
Pict7 = New Bitmap("e:\datos2\platano7.bmp")
'Pict1 = New Bitmap("e:\datos2\N1.bmp")
'Pict2 = New Bitmap("e:\datos2\N2.bmp")
'Pict3 = New Bitmap("e:\datos2\N3.bmp")
'Pict4 = New Bitmap("e:\datos2\N4.bmp")
'Pict5 = New Bitmap("e:\datos2\N5.bmp")
'Pict6 = New Bitmap("e:\datos2\N6.bmp")
'Pict7 = New Bitmap("e:\datos2\N7.bmp")
Grafico5 = PictureBox5.CreateGraphics
Grafico6 = PictureBox6.CreateGraphics
Grafico7 = PictureBox7.CreateGraphics
GraficoCombinado = Cuadro.CreateGraphics
End Sub
Private Sub Iniciar(sender As Object, e As EventArgs) Handles
BtnIniciar.Click
For fila = 0 To alto - 1
For col = 0 To ancho - 1
rojo = Rojos1(fila, col)
verde = Verdes1(fila, col)
azul = Azules1(fila, col)
Brocha1.Color = Color.FromArgb(rojo, verde, azul)
Grafico1.FillRectangle(Brocha1, cx + col, cy + fila, 1, 1)
rojo = Rojos2(fila, col)
verde = Verdes2(fila, col)
azul = Azules2(fila, col)
Brocha2.Color = Color.FromArgb(rojo, verde, azul)
Grafico2.FillRectangle(Brocha2, cx + col, cy + fila, 1, 1)
rojo = Rojos3(fila, col)
verde = Verdes3(fila, col)
azul = Azules3(fila, col)
Brocha3.Color = Color.FromArgb(rojo, verde, azul)
Grafico3.FillRectangle(Brocha3, cx + col, cy + fila, 1, 1)
rojo = Rojos4(fila, col)
verde = Verdes4(fila, col)
azul = Azules4(fila, col)
Brocha4.Color = Color.FromArgb(rojo, verde, azul)
Grafico4.FillRectangle(Brocha4, cx + col, cy + fila, 1, 1)
rojo = Rojos5(fila, col)
verde = Verdes5(fila, col)
azul = Azules5(fila, col)
Brocha5.Color = Color.FromArgb(rojo, verde, azul)
Grafico5.FillRectangle(Brocha5, cx + col, cy + fila, 1, 1)
rojo = Rojos6(fila, col)
verde = Verdes6(fila, col)
azul = Azules6(fila, col)
Brocha6.Color = Color.FromArgb(rojo, verde, azul)
Grafico6.FillRectangle(Brocha6, cx + col, cy + fila, 1, 1)
rojo = Rojos7(fila, col)
verde = Verdes7(fila, col)
Next
Next
Contfactor = 0
End Sub
Private Sub btnMadurar_Click_1(sender As Object, e As EventArgs)
Handles btnMadurar.Click
velocidad = TextBox1.Text
factor = TextBox2.Text
Timer1.Interval = velocidad
Timer1.Enabled = True
End Sub
Private Sub btnDetener_Click(sender As Object, e As EventArgs) Handles
btnDetener.Click
Timer1.Enabled = False
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles
Timer1.Tick
Contfactor = Contfactor + factor
TextBox4.Text = Contfactor
Graficar(sender, e)
End Sub
Sub graficar12(valor As Single)
Dim rojo12, verde12, azul12 As Integer
For fila = 0 To alto - 1
For col = 0 To ancho - 1
rojo12 = Rojos1(fila, col) * (1 - valor) + Rojos2(fila, col) * valor
verde12 = Verdes1(fila, col) * (1 - valor) + Verdes2(fila, col) * valor
azul12 = Azules1(fila, col) * (1 - valor) + Azules2(fila, col) * valor
brochaG.Color = Color.FromArgb(rojo12, verde12, azul12)
GraficoCombinado.FillRectangle(brochaG, cx + col, cy + fila, 1, 1)
Next
Next
End Sub
Sub graficar23(valor As Single)
Dim rojo23, verde23, azul23 As Integer
For fila = 0 To alto - 1
For col = 0 To ancho - 1
rojo23 = Rojos2(fila, col) * (1 - valor) + Rojos3(fila, col) * valor
End Sub
Problema de recorrido
Imports System.IO
Module Module2
Public camino As Integer = 0
Public Const niveles As Integer = 2
Public Const ne As Integer = 6
Public X(ne) As Integer
Public Y(ne) As Integer
Public Const maxfilas As Integer = 20
Public Const maxcol As Integer = 40
Public nf As Integer = 20
Public nc As Integer = 40
Public A(maxfilas, maxcol) As Integer
Public Cx As Integer = 1
Public Cy As Integer = 2
Sub VerMatriz(Cx As Integer, Cy As Integer, A(,) As Integer, nf As Integer, nc
As Integer)
Dim fila, col As Integer
For fila = 0 To nf - 1
For col = 0 To nc - 1
Console.SetCursorPosition(Cx + col, Cy + fila)
Console.ForegroundColor = A(fila, col)
Console.Write("{0}", A(fila, col))
Next
Next
End Sub
Sub ImPrimirSer(Cx As Integer, Cy As Integer, X() As Integer, Y() As Integer,
ne As Integer)
Dim fila As Integer
For fila = 0 To ne - 1
Console.ForegroundColor = 14 - fila
Console.SetCursorPosition(Cx + X(fila), Cy + Y(fila))
Console.Write("{0}", fila + 1)
Next
End Sub
Sub RecuperarMatriz(ByVal nombrearchivo As String, ByVal A(,) As Integer,
ByVal nf As Integer, ByVal nc As Integer)
Dim srLector As StreamReader
srLector = New StreamReader(nombrearchivo)
Dim fila As Integer, col As Integer
Dim cadena As String = ""
Dim subcadena As String
Dim pos As Integer = 0
Dim inicio As Integer = 1
For fila = 0 To nf - 1
cadena = srLector.ReadLine()
cadena = cadena & Chr(9)
inicio = 1
For col = 0 To nc - 1
pos = InStr(inicio, cadena, Chr(9))
subcadena = Mid(cadena, inicio, pos - inicio)
A(fila, col) = Val(subcadena)
inicio = pos + 1
Next
Next
'Form1.ListBox1.Items.Add("Archivo leido satisfactoriamente")
srLector.Close()
End Sub
Function ContarVecinos(Cx As Integer, Cy As Integer, nivel As Integer)
Dim fila, col As Integer
Dim suma As Integer = 0
Dim valor As Integer
Dim r As Integer
For fila = Cy - nivel To Cy + nivel
For col = Cx - nivel To Cx + nivel
valor = A(fila, col)
r = evaluar(col, fila)
If (valor = camino And r = 1) Then suma = suma + 1
Next
Next
Return suma
End Function
Function buscar(cx As Integer, cy As Integer)
Dim fila As Integer
Dim r1 As Integer = 0
For fila = 0 To ne - 1
If (X(fila) = cx And Y(fila) = cy) Then
r1 = 1
Exit For
End If
Next
Return r1
End Function
Function ContarCerca(cx As Integer, cy As Integer, nivel As Integer) As
Integer
Dim fila As Integer
Dim suma As Integer = 0
Dim valor As Integer = 0
' cuanto vecinos mas cercanos t
For fila = cy - nivel To cy + nivel
For col = cx - nivel To cx + nivel
valor = buscar(col, fila)
If (valor = 1) Then suma = suma + 1
Next
Next
Return suma
End Function
Function Evaluar(cx As Integer, cy As Integer) As Integer
Dim r1 As Integer = 0
Dim r2 As Integer = 1
Dim r3 As Integer = 0
If (A(cy, cx) = camino) Then r1 = 1
For fila = 0 To ne - 1
If cx = X(fila) And cy = Y(fila) Then
r2 = 0
Exit For
End If
Next
If (r1 = 1 And r2 = 1) Then r3 = 1
Return r3
End Function
' si hay varios mayores empate si escoge el que tiene menos vecinos
For fila = 0 To 3
If Suma(fila) >= mayor Then
If (smenor(fila) <= menor) Then
menor = smenor(fila)
dirMenor = fila
cont = cont + 1
End If
End If
Next
' si hay mas de dos
For fila = ne - 1 To 1 Step -1
X(fila) = X(fila - 1)
Y(fila) = Y(fila - 1)
Next
If cont = 1 Then
' modificamos
Select Case dir1
Case 1
X(0) = Cx + 1
Case 2
Y(0) = Cy - 1
Case 3
X(0) = Cx - 1
Case 4
Y(0) = Cy + 1
End Select
Else ' si hay empate
Select Case dirMenor + 1
Case 1
X(0) = Cx + 1
Case 2
Y(0) = Cy - 1
Case 3
X(0) = Cx - 1
Case 4
Y(0) = Cy + 1
End Select
End If
Return dir1
End Function
End Module
Module Module1
Sub Main()
Dim Nombre As String = "E:\datos1\matriz40x20.txt"