Ver Mensaje Individual
  #11 (permalink)  
Antiguo 04/05/2016, 10:03
aguml
 
Fecha de Ingreso: febrero-2015
Mensajes: 404
Antigüedad: 9 años, 2 meses
Puntos: 3
Respuesta: recursividad c++ hallar todas las posibles soluciones de la suma de un ve

Se me ocurre algo y no se si seria una locura. Pide que encuentre todas las combinaciones posibles donde se de una condición y se me ocurre que se podría obtener todas las permutaciones posibles y en cada una usa dos bucles donde se va probando si la suma sirve, o sea que sumas el primero con el siguiente y el siguiente... hasta que de él número buscado o hasta que te pases y después haces lo mismo pero empezando desde el segundo y así hasta el final y luego haces una permuta y repites y así hasta realizar todas las permutaciones posibles. Por supuesto habría que desechar las combinaciones que se repitan. ¿Seria buena idea o seria complicarse la vida?
En este código se usa recursividad para permutaciones:
Código C:
Ver original
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /* print a list of ints */
  5. int show(int *x, int len)
  6. {
  7.     int i;
  8.     for (i = 0; i < len; i++)
  9.         printf("%d%c", x[i], i == len - 1 ? '\n' : ' ');
  10.     return 1;
  11. }
  12.  
  13. /* next lexicographical permutation */
  14. int next_lex_perm(int *a, int n) {
  15. #   define swap(i, j) {t = a[i]; a[i] = a[j]; a[j] = t;}
  16.     int k, l, t;
  17.  
  18.     /* 1. Find the largest index k such that a[k] < a[k + 1]. If no such
  19.           index exists, the permutation is the last permutation. */
  20.     for (k = n - 1; k && a[k - 1] >= a[k]; k--);
  21.     if (!k--) return 0;
  22.  
  23.     /* 2. Find the largest index l such that a[k] < a[l]. Since k + 1 is
  24.        such an index, l is well defined */
  25.     for (l = n - 1; a[l] <= a[k]; l--);
  26.  
  27.     /* 3. Swap a[k] with a[l] */
  28.     swap(k, l);
  29.  
  30.     /* 4. Reverse the sequence from a[k + 1] to the end */
  31.     for (k++, l = n - 1; l > k; l--, k++)
  32.         swap(k, l);
  33.     return 1;
  34. #   undef swap
  35. }
  36.  
  37. void perm1(int *x, int n, int callback(int *, int))
  38. {
  39.     do {
  40.         if (callback) callback(x, n);
  41.     } while (next_lex_perm(x, n));
  42. }
  43.  
  44. /* Boothroyd method; exactly N! swaps, about as fast as it gets */
  45. void boothroyd(int *x, int n, int nn, int callback(int *, int))
  46. {
  47.     int c = 0, i, t;
  48.     while (1) {
  49.         if (n > 2) boothroyd(x, n - 1, nn, callback);
  50.         if (c >= n - 1) return;
  51.  
  52.         i = (n & 1) ? 0 : c;
  53.         c++;
  54.         t = x[n - 1], x[n - 1] = x[i], x[i] = t;
  55.         if (callback) callback(x, nn);
  56.     }
  57. }
  58.  
  59. /* entry for Boothroyd method */
  60. void perm2(int *x, int n, int callback(int*, int))
  61. {
  62.     if (callback) callback(x, n);
  63.     boothroyd(x, n, n, callback);
  64. }
  65.  
  66. /* same as perm2, but flattened recursions into iterations */
  67. void perm3(int *x, int n, int callback(int*, int))
  68. {
  69.     /* calloc isn't strictly necessary, int c[32] would suffice
  70.        for most practical purposes */
  71.     int d, i, t, *c = calloc(n, sizeof(int));
  72.  
  73.     /* curiously, with GCC 4.6.1 -O3, removing next line makes
  74.        it ~25% slower */
  75.     if (callback) callback(x, n);
  76.     for (d = 1; ; c[d]++) {
  77.         while (d > 1) c[--d] = 0;
  78.         while (c[d] >= d)
  79.             if (++d >= n) goto done;
  80.  
  81.         t = x[ i = (d & 1) ? c[d] : 0 ], x[i] = x[d], x[d] = t;
  82.         if (callback) callback(x, n);
  83.     }
  84. done:   free(c);
  85. }
  86.  
  87. #define N 4
  88.  
  89. int main()
  90. {
  91.     int i, x[N];
  92.     for (i = 0; i < N; i++) x[i] = i + 1;
  93.  
  94.     /* three different methods */
  95.     perm1(x, N, show);
  96.     perm2(x, N, show);
  97.     perm3(x, N, show);
  98.  
  99.     return 0;
  100. }
Lo saqué de esta página donde lo hay en otros lenguajes: http://rosettacode.org/wiki/Permutations#C.2B.2B

Última edición por aguml; 04/05/2016 a las 10:13