Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Proponga dos algoritmos, uno iterativo y otro recursivo, que dado un vector
- de N numeros y los limites inferior y superior de un intervalo, sume los numeros
- que esten en dicho intervalo. Calcule su orden y diga cual de los 2 algoritmos es
- mejor y porque
- Ej. Vector[2,3,6,9,10], limites 3 y 9
- Suma= 18(3+6+9) Fuera intervalo=2(2 y 10)
- */
- public class ParcialDesarrollo2022 {
- // Variable Global
- public int suma=0;
- public int fIntervalo=0;
- public static void main(String[] args) {
- (new ParcialDesarrollo2022()).run();
- }
- public void run(){
- int [] V={2,3,6,9,10};
- // Llamado recursivo (procedimiento)
- procedimientoRecursivo(V, V.length-1, 3, 9);
- System.out.println("Suma de valores dentro del rango: "+suma);
- System.out.println("Valores fuera del rango: "+fIntervalo);
- //Llamado recursivo (funcion)
- System.out.println("Suma de valores dentro del rango: "+fRecursiva(V, V.length-1, 3, 9));
- System.out.println("Valores fuera del rango: "+fRecursivaFRango(V, V.length-1, 3, 9));
- }
- public int fRecursivaFRango(int[]V ,int N,int I,int S){
- if(N==-1){
- // Criterio de parada
- }else{
- if(V[N]>=I && V[N]<=S){
- return 0+fRecursivaFRango(V, N-1, I, S);
- }else{
- return 1+fRecursivaFRango(V, N-1, I, S);
- }
- }
- return 0;
- }
- public int fRecursiva(int[]V ,int N,int I,int S){
- if(N==-1){
- // Criterio de parada
- }else{
- if(V[N]>=I && V[N]<=S){
- return V[N]+fRecursiva(V, N-1, I, S);
- }else{
- return 0+fRecursiva(V, N-1, I, S);
- }
- }
- return 0;
- }
- public void procedimientoRecursivo(int[]V ,int N,int I,int S){
- if(N==-1){
- // Criterio de parada
- }else{
- if(V[N]>=I && V[N]<=S){
- suma=suma+V[N];
- procedimientoRecursivo(V, N-1, I, S);
- }else{
- fIntervalo++;
- procedimientoRecursivo(V, N-1, I, S);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement