Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.85 KB | None | 0 0
  1. var(
  2. recorrido []int
  3. recorridoIndices []int
  4. )
  5. func main() {
  6.     arreglo := []int{2, -4,7,0,77,-7}
  7.     exercise(arreglo)
  8. }
  9.  
  10. func exercise(arreglo []int){  
  11.     tamaño := len(arreglo)
  12.     saltos := 0
  13.     indiceActual := 0
  14.     i := 0
  15.     for(arreglo[indiceActual] != 0){
  16.         elementoActual := arreglo[indiceActual]
  17.         nuevoArreglo := []int{elementoActual}
  18.         nuevoArregloIndices := []int{indiceActual}
  19.         recorridoIndices = append(recorridoIndices,nuevoArregloIndices...)
  20.         recorrido = append(recorrido,nuevoArreglo...)
  21.         indiceActual = devolverIndice(tamaño,indiceActual,elementoActual)
  22.         saltos = saltos + i
  23.         i++
  24.         if (haybucle()){
  25.             fmt.Println("Se ha encontrado un bucle")
  26.             fmt.Println("Cantidad de saltos ", saltos)
  27.             fmt.Println("Recorrido: ",recorrido)
  28.            
  29.             return
  30.         }
  31.     }
  32.     fmt.Println("Cantidad de saltos ", saltos)
  33.     fmt.Println("Recorrido: ",recorrido)
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement