Advertisement
Guest User

Cálculo de billetes para cajero automático por backtracking

a guest
Dec 2nd, 2010
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.27 KB | None | 0 0
  1. /*****************************************************************************
  2. * Contact : codebits@esdis.es                                                *
  3. * This program is free software; you can redistribute it and/or              *
  4. * modify it under the terms of the GNU General Public License                *
  5. * version 3 as published by the Free Software Foundation.                    *
  6. *                                                                            *
  7. * This program is distributed in the hope that it will be useful,            *
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
  10. * GNU General Public License for more details.                               *
  11. *                                                                            *
  12. * You should have received a copy of the GNU General Public License          *
  13. * along with this program; if not, write to the Free Software                *
  14. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, *
  15. * USA.                                                                       *
  16. *                                                                            *
  17. * The above copyright notice and this permission notice shall be included in *
  18. * all copies or substantial portions of the Software.                        *
  19. *****************************************************************************/
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. // Desde aqui se puede tocar
  23. int billetes_atm[7]={1,1,0,0,0,0,1000};     // Numero de billetes de cada tipo, coincidir en tamaño con otros vectores
  24. int billetes_valor[7]={500,200,100,50,20,10,5}; // Valor de los billetes
  25. int billetes_resultado[7]={0,0,0,0,0,0,0};  // Vector donde va el resultado
  26. int objetivo=5000;              // Target, cantidad de dinero a retirar. Es un INT, máximo 32767, por favor
  27. // Hasta aqui se puede tocar
  28.  
  29. int max_billetes;
  30. int profundidad=0;
  31.  
  32. void echar_billetes() {
  33.     int size, x, t_linea, t_total=0;
  34.     size=sizeof(billetes_atm) / sizeof(billetes_atm[0]);
  35.     for(x=0 ; x<size ; x++) {
  36.         if(billetes_resultado[x]) {
  37.             t_linea=billetes_valor[x] * billetes_resultado[x];
  38.             t_total+=t_linea;
  39.             printf("Billetes de %d\tx %d\t= %d\n", billetes_valor[x], billetes_resultado[x], t_linea);
  40.         }
  41.     }
  42.     printf("---------------------------------\n");
  43.     printf("Total\t\t\t= %d\n", t_total);
  44. }
  45.  
  46. void calculo_billetes(int objetivo) {
  47.     int size, x, y;
  48.     size=sizeof(billetes_atm) / sizeof(billetes_atm[0]);
  49.     if(profundidad>=max_billetes) exit(-1); // No solución
  50.     for(x=0 ; x<size ; x++) {
  51.         if(billetes_valor[x]<=objetivo && billetes_atm[x]>0) {
  52.             billetes_atm[x]--; billetes_resultado[x]++; objetivo-=billetes_valor[x]; profundidad++;
  53.             if(!objetivo) {
  54.                 echar_billetes(); // Solución encontrada
  55.                 exit(-1);
  56.             } else {
  57.                 calculo_billetes(objetivo);
  58.                 billetes_atm[x]++; billetes_resultado[x]--; objetivo+=billetes_valor[x]; profundidad--;
  59.             }
  60.         }
  61.     }
  62. }
  63.  
  64. int main() {
  65.     int size, x;
  66.     size=sizeof(billetes_atm) / sizeof(billetes_atm[0]);
  67.     for(x=0 ; x<size ; x++) {
  68.         max_billetes+=billetes_atm[x]; // Calculo del numero de billetes que hay en el cajero
  69.     }
  70.     calculo_billetes(objetivo); // Llamada a la función recursiva
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement