Ver Mensaje Individual
  #72 (permalink)  
Antiguo 03/12/2014, 06:22
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.

Lo modifico de forma que también encuentre otros colmillos si es que existen:

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. int dec[5] = {1, 10, 100, 1000, 10000};
  7.  
  8. int cant(int num, int t)
  9. {
  10.     if (num) t +=  1 << (num%10) * 6, cant(num / 10, t);
  11.     else return t;
  12. }
  13. int comprobar(int min, int max, int num, int b, int t, int es)
  14. {
  15.     if(min <= max)
  16.     {
  17.         b = num / min;
  18.         if (min * b == num && ((min % 10) || (b%10))
  19.             && t == cant(min, 0) + cant(b, 0) )
  20.             printf("%d = %d x %d\n", num, min, b), es++;
  21.         comprobar(min + 1, max, num, b, t, es);
  22.     }else return es > 0 ? 1 : 0;
  23. }
  24. int es_vampiro(int num)
  25. {
  26.     int min, max, b = 0, nd = log10(num) + 1, t = cant(num, 0);
  27.     if (nd % 2) return 0;
  28.     nd /= 2;
  29.     min = MAX(dec[nd - 1], (num + dec[nd] - 2)/(dec[nd] - 1));
  30.     max = MIN(num/min, sqrt(num));
  31.     return comprobar(min, max, num, b, t, 0);
  32. }
  33. int main(void)
  34. {
  35.     printf("\n<%d>\n", es_vampiro(13078260));
  36.     return(0);
  37. }

Saludos