Advertisement
MOISES_QUISPE_ROJAS

Untitled

Jun 5th, 2022
824
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     Proponga dos algoritmos, uno iterativo y otro recursivo, que dado un vector
  3.     de N numeros y los limites inferior y superior de un intervalo, sume los numeros
  4.     que esten en dicho intervalo. Calcule su orden y diga cual de los 2 algoritmos es
  5.     mejor y porque
  6.    
  7.     Ej. Vector[2,3,6,9,10], limites 3 y 9
  8.     Suma= 18(3+6+9)     Fuera intervalo=2(2 y 10)
  9. */
  10.  
  11. public class ParcialDesarrollo2022 {
  12.     // Variable Global
  13.     public int suma=0;
  14.     public int fIntervalo=0;
  15.    
  16.     public static void main(String[] args) {
  17.         (new ParcialDesarrollo2022()).run();
  18.     }
  19.    
  20.     public void run(){
  21.         int [] V={2,3,6,9,10};        
  22.         // Llamado recursivo (procedimiento)
  23.         procedimientoRecursivo(V, V.length-1, 3, 9);
  24.         System.out.println("Suma de valores dentro del rango: "+suma);
  25.         System.out.println("Valores fuera del rango: "+fIntervalo);
  26.         //Llamado recursivo (funcion)
  27.         System.out.println("Suma de valores dentro del rango: "+fRecursiva(V, V.length-1, 3, 9));
  28.         System.out.println("Valores fuera del rango: "+fRecursivaFRango(V, V.length-1, 3, 9));
  29.     }
  30.    
  31.     public int fRecursivaFRango(int[]V ,int N,int I,int S){
  32.         if(N==-1){
  33.             // Criterio de parada
  34.         }else{
  35.             if(V[N]>=I && V[N]<=S){                                
  36.                 return 0+fRecursivaFRango(V, N-1, I, S);
  37.             }else{                
  38.                 return 1+fRecursivaFRango(V, N-1, I, S);                
  39.             }
  40.         }
  41.         return 0;
  42.     }
  43.    
  44.     public int fRecursiva(int[]V ,int N,int I,int S){
  45.         if(N==-1){
  46.             // Criterio de parada
  47.         }else{
  48.             if(V[N]>=I && V[N]<=S){                                
  49.                 return V[N]+fRecursiva(V, N-1, I, S);
  50.             }else{                
  51.                 return 0+fRecursiva(V, N-1, I, S);                
  52.             }
  53.         }
  54.         return 0;
  55.     }
  56.    
  57.     public void procedimientoRecursivo(int[]V ,int N,int I,int S){        
  58.         if(N==-1){
  59.             // Criterio de parada
  60.         }else{
  61.             if(V[N]>=I && V[N]<=S){                
  62.                 suma=suma+V[N];
  63.                 procedimientoRecursivo(V, N-1, I, S);
  64.             }else{                
  65.                 fIntervalo++;
  66.                 procedimientoRecursivo(V, N-1, I, S);
  67.             }
  68.         }
  69.     }
  70.    
  71. }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement