Ver Mensaje Individual
  #68 (permalink)  
Antiguo 02/12/2014, 17:08
kutcher
 
Fecha de Ingreso: noviembre-2014
Mensajes: 36
Antigüedad: 9 años, 6 meses
Puntos: 13
Respuesta: Petando la pila. Problemas y retos usando recursividad.

Buenas, al fin vuelvo a pasar por aquí

Solución al primer ejercicio:

Código C++:
Ver original
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. char* no_Rep(char *dst, int i, int len)
  6. {
  7.     int flag = 0;
  8.     if (i < len)
  9.     {
  10.         dst[i] == dst[i - 1] ? (flag = 1) : (flag = 0);
  11.         if (flag)
  12.         {
  13.             memmove(dst + i - 1, dst + i, len - i * sizeof(char));
  14.             i -= 1, len -= 1;
  15.         }
  16.         no_Rep(dst, i + 1, len);
  17.     }
  18.     else
  19.         dst[i] = '\0';
  20.     return dst;
  21. }
  22.  
  23. char* noRep(char* destino, const char* origen)
  24. {
  25.     strcpy(destino, origen);
  26.     return no_Rep(destino, 1, strlen(destino));
  27. }
  28.  
  29. int main(void)
  30. {
  31.     char cad[] = "abccccfabaddeff";
  32.     char* destino = calloc(strlen(cad) + 1, sizeof(char));
  33.  
  34.     printf("%s\n", noRep(destino, cad));
  35.  
  36.     free(destino);
  37.     return 0;
  38. }

Solución al segundo ejercicio:

Código C++:
Ver original
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. #define MAX(a,b) ((a)>(b)?(a):(b))
  5. #define MIN(a,b) ((a)<(b)?(a):(b))
  6.  
  7. int dec[5];
  8.  
  9. int cant(int x, int t)
  10. {
  11.     if (x)
  12.        t +=  1 << (x % 10) * 6, cant(x / 10, t);
  13.     else
  14.         return t;
  15. }
  16.  
  17. int comprobar(int min, int max, int x, int b, int t)
  18. {
  19.     if(min <= max)
  20.     {
  21.         b = x / min;
  22.         if (min * b == x && ((min % 10) || (b%10))
  23.             && t == cant(min, 0) + cant(b, 0) )
  24.         {
  25.             printf("%d = %d x %d ", x, min, b);
  26.             return 1;
  27.         }
  28.         comprobar(min + 1, max, x, b, t);
  29.     }
  30.     else return 0;
  31. }
  32.  
  33. int es_vampiro(int x)
  34. {
  35.     int min, max, b = 0, nd = log10(x) + 1, t = cant(x, 0);
  36.     if (nd % 2) return 0;
  37.  
  38.     min = MAX(dec[nd/2-1], (x+dec[nd/2]-2)/(dec[nd/2]-1));
  39.     max = MIN(x/min, sqrt(x));
  40.  
  41.     return comprobar(min, max, x, b, t);
  42. }
  43.  
  44. int main(void)
  45. {
  46.     dec[0] = 1;
  47.  
  48.     for (int i = 1; i < 5; i++) dec[i] = dec[i - 1] * 10;
  49.     printf("\n%d\n", es_vampiro(13078260));
  50.  
  51.     return(0);
  52. }

Saludos

Última edición por kutcher; 02/12/2014 a las 18:51