Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var(
- recorrido []int
- recorridoIndices []int
- )
- func main() {
- arreglo := []int{2, -4,7,0,77,-7}
- exercise(arreglo)
- }
- func exercise(arreglo []int){
- tamaño := len(arreglo)
- saltos := 0
- indiceActual := 0
- i := 0
- for(arreglo[indiceActual] != 0){
- elementoActual := arreglo[indiceActual]
- nuevoArreglo := []int{elementoActual}
- nuevoArregloIndices := []int{indiceActual}
- recorridoIndices = append(recorridoIndices,nuevoArregloIndices...)
- recorrido = append(recorrido,nuevoArreglo...)
- indiceActual = devolverIndice(tamaño,indiceActual,elementoActual)
- saltos = saltos + i
- i++
- if (haybucle()){
- fmt.Println("Se ha encontrado un bucle")
- fmt.Println("Cantidad de saltos ", saltos)
- fmt.Println("Recorrido: ",recorrido)
- return
- }
- }
- fmt.Println("Cantidad de saltos ", saltos)
- fmt.Println("Recorrido: ",recorrido)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement